digitalmars.D.learn - Creating Anonymous structs
- Alex (21/21) May 18 2019 Structs combine data, I have a use case where I do not want to
- Alex (8/8) May 18 2019 Also, I realize one could use Voldemort types, e.g., something
- Adam D. Ruppe (4/7) May 18 2019 This is basically what Phobos's Tuple is.
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
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
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
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: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.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
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: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.On Saturday, 18 May 2019 at 15:59:05 UTC, Alex wrote: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.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