www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Is there something like anonymous struct?

reply "eGust" <egustc gmail.com> writes:
I need something to store some functions, and it only will be 
used once. There is my code now:

struct Funcs {
auto
	func1	= &fn1,
	func2	= &fn2,
...
	funcN	= &fnN;
}

__gshared immutable Funcs foo;

export extern(Windows) auto bar()
{
	return &foo;
}

============

Can I write something like this:

__gshared immutable foo = {
auto
	func1	= &fn1,
	func2	= &fn2,
...
}

Or just

... = {
   &fn1, &fn2, ...
}
Mar 04 2013
parent reply "anonymous" <anonymous example.com> writes:
On Tuesday, 5 March 2013 at 02:01:47 UTC, eGust wrote:
 Can I write something like this:

 __gshared immutable foo = {
 auto
 	func1	= &fn1,
 	func2	= &fn2,
 ...
 }

 Or just

 ... = {
   &fn1, &fn2, ...
 }
import std.typecons: tuple; __gshared immutable foo = tuple(&fn1, &fn2, ...);
Mar 04 2013
next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 04 Mar 2013 21:15:27 -0500, anonymous <anonymous example.com>  
wrote:

 On Tuesday, 5 March 2013 at 02:01:47 UTC, eGust wrote:
 Can I write something like this:

 __gshared immutable foo = {
 auto
 	func1	= &fn1,
 	func2	= &fn2,
 ...
 }

 Or just

 ... = {
   &fn1, &fn2, ...
 }
import std.typecons: tuple; __gshared immutable foo = tuple(&fn1, &fn2, ...);
That doesn't name them. I think you have to be more verbose when naming the members: __gshared immutable foo = Tuple!(typeof(&fn1), "func1", typeof(&fn2), "func2")(&fn1, &fn2); Wow, that really sucks. Is there a better way? I bet a mixin could help here... -Steve
Mar 04 2013
parent Jacob Carlborg <doob me.com> writes:
On 2013-03-05 03:33, Steven Schveighoffer wrote:

 That doesn't name them.  I think you have to be more verbose when naming
 the members:

 __gshared immutable foo = Tuple!(typeof(&fn1), "func1", typeof(&fn2),
 "func2")(&fn1, &fn2);

 Wow, that really sucks.  Is there a better way?  I bet a mixin could
 help here...
This is a proposal I had before: http://forum.dlang.org/thread/kfbnuc$1cro$1 digitalmars.com -- /Jacob Carlborg
Mar 04 2013
prev sibling parent reply "eGust" <egustc gmail.com> writes:
On Tuesday, 5 March 2013 at 02:15:33 UTC, anonymous wrote:
 On Tuesday, 5 March 2013 at 02:01:47 UTC, eGust wrote:
 Can I write something like this:

 __gshared immutable foo = {
 auto
 	func1	= &fn1,
 	func2	= &fn2,
 ...
 }

 Or just

 ... = {
  &fn1, &fn2, ...
 }
import std.typecons: tuple; __gshared immutable foo = tuple(&fn1, &fn2, ...);
Yes, it looks better. Can tuple guarantee the order and alignment of the elements? I need an interface with other languages.
Mar 04 2013
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Mon, 04 Mar 2013 21:58:57 -0500, eGust <egustc gmail.com> wrote:

 On Tuesday, 5 March 2013 at 02:15:33 UTC, anonymous wrote:
 On Tuesday, 5 March 2013 at 02:01:47 UTC, eGust wrote:
 Can I write something like this:

 __gshared immutable foo = {
 auto
 	func1	= &fn1,
 	func2	= &fn2,
 ...
 }

 Or just

 ... = {
  &fn1, &fn2, ...
 }
import std.typecons: tuple; __gshared immutable foo = tuple(&fn1, &fn2, ...);
Yes, it looks better. Can tuple guarantee the order and alignment of the elements? I need an interface with other languages.
It will be equivalent to a struct declared with those members. However, I would say if you are looking for a type to push across languages, why not declare the type? -Steve
Mar 05 2013
parent "eGust" <egustc gmail.com> writes:
On Tuesday, 5 March 2013 at 16:02:50 UTC, Steven Schveighoffer 
wrote:
 On Mon, 04 Mar 2013 21:58:57 -0500, eGust <egustc gmail.com> 
 wrote:

 On Tuesday, 5 March 2013 at 02:15:33 UTC, anonymous wrote:
 On Tuesday, 5 March 2013 at 02:01:47 UTC, eGust wrote:
 Can I write something like this:

 __gshared immutable foo = {
 auto
 	func1	= &fn1,
 	func2	= &fn2,
 ...
 }

 Or just

 ... = {
 &fn1, &fn2, ...
 }
import std.typecons: tuple; __gshared immutable foo = tuple(&fn1, &fn2, ...);
Yes, it looks better. Can tuple guarantee the order and alignment of the elements? I need an interface with other languages.
It will be equivalent to a struct declared with those members. However, I would say if you are looking for a type to push across languages, why not declare the type? -Steve
Thank you all guys. I'll use tuple. This way will make it faster and easier. If QtD can be built with 2.062, I would do all my work in D. Now I'm using CPP which is I'd never used before. It already bothers me. Now I'm writing C-style CPP for GUI and D for all the others in the same time, so I must try to reduce the number of the interface functions. That's why I don't wanna waste my time on repeating the same declarations, especially it's a trouble if I make any change. Because it's easy to output the type of var in D, I think I can make them declared in the end. :)
Mar 05 2013