Thread overview
Default initialization of struct
Feb 13, 2013
Daniel Kozak
Feb 13, 2013
Jacob Carlborg
Feb 13, 2013
Daniel Kozak
Feb 13, 2013
simendsjo
Feb 13, 2013
Daniel Kozak
February 13, 2013
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")
}

February 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 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
February 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.
February 13, 2013
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

February 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]);
}