Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
May 18, 2019 Creating Anonymous structs | ||||
---|---|---|---|---|
| ||||
Structs combine data, I have a use case where I do not want to litter the scope with variables and would like to put them in a struct but the variable will only be used once. struct X { int x; double y; } X x; Seems redundant to have to do this, rather, it would be nice to do auto x = struct { int x; double y; }; And this would create the same effect but it would only exist in one place. Is there any way to accomplish this without actually defeating the purpose? Obviously there is not a huge difference between what is possible and what I want to achieve. I just feel it would be more sound to use an anonymous struct. |
May 18, 2019 Re: Creating Anonymous structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alex | Also, I realize one could use Voldemort types, e.g., something like auto x = (){ struct X { int x; } return X(); } but this is so verbose as to not really be any better(although it does accomplish hiding the struct, I'm not so concerned with hiding the struct as I am code brevity. I do realize there is very little extra chars but that isn't the point. I'm asking if there is a way to accomplish what I'd like to do). |
May 18, 2019 Re: Creating Anonymous structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alex | On Saturday, 18 May 2019 at 15:59:05 UTC, Alex wrote: > Structs combine data, I have a use case where I do not want to litter the scope with variables and would like to put them in a struct but the variable will only be used once. This is basically what Phobos's Tuple is. auto x = tuple(value_for_x, value_for_y); http://dpldocs.info/experimental-docs/std.typecons.tuple.html |
May 18, 2019 Re: Creating Anonymous structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Saturday, 18 May 2019 at 20:03:14 UTC, Adam D. Ruppe wrote:
> On Saturday, 18 May 2019 at 15:59:05 UTC, Alex wrote:
>> Structs combine data, I have a use case where I do not want to litter the scope with variables and would like to put them in a struct but the variable will only be used once.
>
> This is basically what Phobos's Tuple is.
>
> auto x = tuple(value_for_x, value_for_y);
>
> http://dpldocs.info/experimental-docs/std.typecons.tuple.html
I thought about using them but they are not the same. A tuple cannot have functionality. It's not as general as a struct or class and it doesn't look as nice if the struct is large... and if we were to use a class we don't get to inherit.
|
May 18, 2019 Re: Creating Anonymous structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Alex | On Saturday, 18 May 2019 at 20:53:50 UTC, Alex wrote:
> On Saturday, 18 May 2019 at 20:03:14 UTC, Adam D. Ruppe wrote:
>> On Saturday, 18 May 2019 at 15:59:05 UTC, Alex wrote:
>>> Structs combine data, I have a use case where I do not want to litter the scope with variables and would like to put them in a struct but the variable will only be used once.
>>
>> This is basically what Phobos's Tuple is.
>>
>> auto x = tuple(value_for_x, value_for_y);
>>
>> http://dpldocs.info/experimental-docs/std.typecons.tuple.html
>
> I thought about using them but they are not the same. A tuple cannot have functionality. It's not as general as a struct or class and it doesn't look as nice if the struct is large... and if we were to use a class we don't get to inherit.
I suppose one could use delegates but it's still a pain because to setup and ends up being more terse than just going about it the standard way.
|
Copyright © 1999-2021 by the D Language Foundation