www.digitalmars.com         C & C++   DMDScript  

D - Neat trick: Constructor for struct or union

reply Stewart Gordon <smjg_1998 yahoo.com> writes:
I've just noticed that there was a thread on struct constructors I 
overlooked about a month ago, but it doesn't seem to be all there when I 
look now.

Well, I discovered this little trick last night and I like it.

You can do something like

struct Qwert {
	int asdfg;

	static Qwert opCall(int a) {
		Qwert hjkl;
		hjkl.asdfg = a;
		return hjkl;
	}
}

Then you effectively have a constructor for the struct, and you can call 
it just like one syntax of C++ constructor:

	Qwert zxcvb = Qwert(42);
	nm(Qwert(105));

So you can effectively do what you can do with class constructors, 
without the overhead of instantiating a class.  It's a bit like defining 
a syntax for a literal of struct type, making it possible to assign 
straight to one where you can't use the static initialiser syntax.

And unlike C++ constructors, defining one doesn't automatically undefine 
the static initialiser syntax, so the programmer still has the 
flexibility of having either available.

Of course you could define a static method with any old name, like the 
old 'factory methods' in some Java classes that are capable of doing 
some of the things constructors aren't (like reusing instances).  My 
idea is basically the same as this, but with the syntactic sugar of 
Qwert(69) rather than Qwert.instance(69).

Stewart.

-- 
My e-mail is valid but not my primary mailbox, aside from its being the 
unfortunate victim of intensive mail-bombing at the moment.  Please keep 
replies on the 'group where everyone may benefit.
Mar 10 2004
next sibling parent Andy Friesen <andy ikagames.com> writes:
Stewart Gordon wrote:
 You can do something like
 
 struct Qwert {
     int asdfg;
 
     static Qwert opCall(int a) {
         Qwert hjkl;
         hjkl.asdfg = a;
         return hjkl;
     }
 }
 
 Then you effectively have a constructor for the struct, and you can call 
 it just like one syntax of C++ constructor:
 
     Qwert zxcvb = Qwert(42);
     nm(Qwert(105));
Slick! -- andy
Mar 10 2004
prev sibling next sibling parent "Carlos Santander B." <carlos8294 msn.com> writes:
"Stewart Gordon" <smjg_1998 yahoo.com> wrote in message
news:c2nllg$1nog$1 digitaldaemon.com
| ...
| Then you effectively have a constructor for the struct,
| and you can call it just like one syntax of C++
| constructor:
|
| Qwert zxcvb = Qwert(42);
| nm(Qwert(105));
| ...
|
| Stewart.
|

Not that you'd need it, but if you want C++ syntax, you can do that for
classes too.

-----------------------
Carlos Santander Bernal
Mar 10 2004
prev sibling next sibling parent reply "Carlos Santander B." <carlos8294 msn.com> writes:
"Stewart Gordon" <smjg_1998 yahoo.com> wrote in message
news:c2nllg$1nog$1 digitaldaemon.com
| I've just noticed that there was a thread on struct
| constructors I overlooked about a month ago, but it
| doesn't seem to be all there when I look now.
|
| Well, I discovered this little trick last night and I
| like it.
|
| You can do something like
|
| struct Qwert {
| int asdfg;
|
| static Qwert opCall(int a) {
| Qwert hjkl;
| hjkl.asdfg = a;
| return hjkl;
| }
| }
|
| Then you effectively have a constructor for the struct,
| and you can call it just like one syntax of C++
| constructor:
|
| Qwert zxcvb = Qwert(42);
| nm(Qwert(105));
|
| So you can effectively do what you can do with class
| constructors, without the overhead of instantiating a
| class.  It's a bit like defining a syntax for a literal
| of struct type, making it possible to assign straight to
| one where you can't use the static initialiser syntax.
|
| And unlike C++ constructors, defining one doesn't
| automatically undefine the static initialiser syntax, so
| the programmer still has the flexibility of having either
| available.
|
| Of course you could define a static method with any old
| name, like the old 'factory methods' in some Java classes
| that are capable of doing some of the things constructors
| aren't (like reusing instances).  My idea is basically
| the same as this, but with the syntactic sugar of
| Qwert(69) rather than Qwert.instance(69).
|
| Stewart.
|
| --
| My e-mail is valid but not my primary mailbox, aside from
| its being the unfortunate victim of intensive
| mail-bombing at the moment.  Please keep replies on the
| 'group where everyone may benefit.

However, using this doesn't let you properly overload opCall:

import std.c.stdio;
struct A {
    static A opCall(int a) { A r; r.aa=a; return r; }
    void opCall() { printf("a=%d\n",aa); }
    int aa;
}
void main() {
    A a=A(34);
    a(); //<-Error
}

Produces: "Error: expected 1 arguments, not 0".

-----------------------
Carlos Santander Bernal
Mar 10 2004
parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Carlos Santander B. wrote:

<snip>
 import std.c.stdio;
 struct A {
     static A opCall(int a) { A r; r.aa=a; return r; }
     void opCall() { printf("a=%d\n",aa); }
     int aa;
 }
 void main() {
     A a=A(34);
     a(); //<-Error
 }
 
 Produces: "Error: expected 1 arguments, not 0".
Ah, that looks like a compiler bug to me. Possible diagnoses I can see: (a) confused by static and non-static methods with same name (b) operator overloading not fully implemented for structs (c) confused by multiple versions of opCall with different parameter lists (d) some linear combination of (a), (b) and (c). I'll be able to experiment when I get back onto my PC.... Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit.
Mar 11 2004
prev sibling parent reply "C. Sauls" <ibisbasenji yahoo.com> writes:
Inspire moment, to be sure.  That's getting written down in my big book 
o' snippets.

-C. Sauls
-Invironz

Stewart Gordon wrote:
 I've just noticed that there was a thread on struct constructors I 
 overlooked about a month ago, but it doesn't seem to be all there when I 
 look now.
 
 Well, I discovered this little trick last night and I like it.
 
 You can do something like
 
 struct Qwert {
     int asdfg;
 
     static Qwert opCall(int a) {
         Qwert hjkl;
         hjkl.asdfg = a;
         return hjkl;
     }
 }
 
 Then you effectively have a constructor for the struct, and you can call 
 it just like one syntax of C++ constructor:
 
     Qwert zxcvb = Qwert(42);
     nm(Qwert(105));
 
 So you can effectively do what you can do with class constructors, 
 without the overhead of instantiating a class.  It's a bit like defining 
 a syntax for a literal of struct type, making it possible to assign 
 straight to one where you can't use the static initialiser syntax.
 
 And unlike C++ constructors, defining one doesn't automatically undefine 
 the static initialiser syntax, so the programmer still has the 
 flexibility of having either available.
 
 Of course you could define a static method with any old name, like the 
 old 'factory methods' in some Java classes that are capable of doing 
 some of the things constructors aren't (like reusing instances).  My 
 idea is basically the same as this, but with the syntactic sugar of 
 Qwert(69) rather than Qwert.instance(69).
 
 Stewart.
 
Mar 11 2004
parent Ilya Minkov <minkov cs.tum.edu> writes:
Snippets are cool. Would you like to publish them?

-eye


C. Sauls schrieb:

 Inspire moment, to be sure.  That's getting written down in my big book 
 o' snippets.
 
 -C. Sauls
 -Invironz
 
 Stewart Gordon wrote:
 
 I've just noticed that there was a thread on struct constructors I 
 overlooked about a month ago, but it doesn't seem to be all there when 
 I look now.

 Well, I discovered this little trick last night and I like it.

 You can do something like

 struct Qwert {
     int asdfg;

     static Qwert opCall(int a) {
         Qwert hjkl;
         hjkl.asdfg = a;
         return hjkl;
     }
 }

 Then you effectively have a constructor for the struct, and you can 
 call it just like one syntax of C++ constructor:

     Qwert zxcvb = Qwert(42);
     nm(Qwert(105));

 So you can effectively do what you can do with class constructors, 
 without the overhead of instantiating a class.  It's a bit like 
 defining a syntax for a literal of struct type, making it possible to 
 assign straight to one where you can't use the static initialiser syntax.

 And unlike C++ constructors, defining one doesn't automatically 
 undefine the static initialiser syntax, so the programmer still has 
 the flexibility of having either available.

 Of course you could define a static method with any old name, like the 
 old 'factory methods' in some Java classes that are capable of doing 
 some of the things constructors aren't (like reusing instances).  My 
 idea is basically the same as this, but with the syntactic sugar of 
 Qwert(69) rather than Qwert.instance(69).

 Stewart.
Mar 11 2004