Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
March 05, 2013 Is there something like anonymous struct? | ||||
---|---|---|---|---|
| ||||
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, ... } |
March 05, 2013 Re: Is there something like anonymous struct? | ||||
---|---|---|---|---|
| ||||
Posted in reply to eGust | 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, ...);
|
March 05, 2013 Re: Is there something like anonymous struct? | ||||
---|---|---|---|---|
| ||||
Posted in reply to anonymous | 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
|
March 05, 2013 Re: Is there something like anonymous struct? | ||||
---|---|---|---|---|
| ||||
Posted in reply to anonymous | 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.
|
March 05, 2013 Re: Is there something like anonymous struct? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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 |
March 05, 2013 Re: Is there something like anonymous struct? | ||||
---|---|---|---|---|
| ||||
Posted in reply to eGust | 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
|
March 06, 2013 Re: Is there something like anonymous struct? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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. :)
|
Copyright © 1999-2021 by the D Language Foundation