www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Default initialization of struct

reply "Daniel Kozak" <kozzi11 gmail.com> writes:
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
next sibling parent Jacob Carlborg <doob me.com> writes:
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 have

How 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
prev sibling next sibling parent "simendsjo" <simendsjo gmail.com> writes:
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
prev sibling next sibling parent "Daniel Kozak" <kozzi11 gmail.com> writes:
On Wednesday, 13 February 2013 at 10:38:23 UTC, Jacob Carlborg 
wrote:
 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 have

How 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

my fault, entity3 should be anonymous struct not type of Entity, just like your proposal
Feb 13 2013
prev sibling parent "Daniel Kozak" <kozzi11 gmail.com> writes:
 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