digitalmars.D - Default initialization of struct
- Daniel Kozak (16/16) Feb 13 2013 Hi all,
- Jacob Carlborg (7/21) Feb 13 2013 How would this work. What if there's another struct in scope that has
- Daniel Kozak (4/30) Feb 13 2013 my fault, entity3 should be anonymous struct not type of Entity,
- simendsjo (7/23) Feb 13 2013 entity3: What should the compiler do? Guess the type as long as
- Daniel Kozak (33/37) Feb 13 2013 No, I dont want named parameters, because I agree it is not good
Hi all, I play with structs and UDAs today, and I found out some limitation: struct Entity { string name; } void main(string[] args) { Entity entity = {name : "Name"}; // static initialization by name auto entity2 = Entity(); // default initialization auto entity3 = {name : "Name"}; // should be nice to have auto entity4 = Entity(name : "Name"); // and this would be useful too. Eg.: for UDAs Entity(name : "EntityName") }
Feb 13 2013
On 2013-02-13 09:49, Daniel Kozak wrote:Hi all, I play with structs and UDAs today, and I found out some limitation: struct Entity { string name; } void main(string[] args) { Entity entity = {name : "Name"}; // static initialization by name auto entity2 = Entity(); // default initialization auto entity3 = {name : "Name"}; // should be nice to haveHow would this work. What if there's another struct in scope that has the same member(s)?auto entity4 = Entity(name : "Name"); // and this would be useful too. Eg.: for UDAs Entity(name : "EntityName") }Check my proposal for anonymous structs: http://forum.dlang.org/thread/kfbnuc$1cro$1 digitalmars.com#post-kfbnuc:241cro:241:40digitalmars.com -- /Jacob Carlborg
Feb 13 2013
On Wednesday, 13 February 2013 at 10:38:23 UTC, Jacob Carlborg wrote:On 2013-02-13 09:49, Daniel Kozak wrote:my fault, entity3 should be anonymous struct not type of Entity, just like your proposalHi all, I play with structs and UDAs today, and I found out some limitation: struct Entity { string name; } void main(string[] args) { Entity entity = {name : "Name"}; // static initialization by name auto entity2 = Entity(); // default initialization auto entity3 = {name : "Name"}; // should be nice to haveHow would this work. What if there's another struct in scope that has the same member(s)?auto entity4 = Entity(name : "Name"); // and this would be useful too. Eg.: for UDAs Entity(name : "EntityName") }Check my proposal for anonymous structs: http://forum.dlang.org/thread/kfbnuc$1cro$1 digitalmars.com#post-kfbnuc:241cro:241:40digitalmars.com
Feb 13 2013
On Wednesday, 13 February 2013 at 08:49:41 UTC, Daniel Kozak wrote:Hi all, I play with structs and UDAs today, and I found out some limitation: struct Entity { string name; } void main(string[] args) { Entity entity = {name : "Name"}; // static initialization by name auto entity2 = Entity(); // default initialization auto entity3 = {name : "Name"}; // should be nice to have auto entity4 = Entity(name : "Name"); // and this would be useful too. Eg.: for UDAs Entity(name : "EntityName") }entity3: What should the compiler do? Guess the type as long as it has a constructor with "name"? entity4: You're talking about static opCall and named parameters. I'm pretty sure named parameters have been rejected as parameter names would be part of the public interface for functions.
Feb 13 2013
entity4: You're talking about static opCall and named parameters. I'm pretty sure named parameters have been rejected as parameter names would be part of the public interface for functions.No, I dont want named parameters, because I agree it is not good idea to have parameter names part of public interface. I speak about syntax sugar, how to do this: module main; import std.stdio; struct Entity { string name; } static Entity entity = {name: "EntityName"}; entity class A { } void main(string[] args) { auto attributes = __traits(getAttributes, A); writeln(attributes[0]); } by this code: module main; import std.stdio; struct Entity { string name; } Entity(name : "EntityName") class A { } void main(string[] args) { auto attributes = __traits(getAttributes, A); writeln(attributes[0]); }
Feb 13 2013