www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Why are non-static struct initializers illegal?

reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
struct A
{
 int x, y;
}

void main()
{
 static A a={5,10};
 A b={5,10};
}

The first line in main() is fine; the second isn't.

Why not?  Structs are value types, yes?  So how is

A b={5,10};

any different from:

int x=5;

?

For that matter, how is this different than using an initializer:

A b;
b.x=5;
b.y=10;

I can't find any compelling reason for this to be illegal. 
Jun 27 2005
next sibling parent reply "Unknown W. Brackets" <unknown simplemachines.org> writes:
This is why, to my understanding:

struct foo
{
    int x, y;
}
struct bar
{
    int x, y;
}

void foobar(foo f)
{
}

void foobar(bar b)
{
}

int main()
{
    foobar({0, 0});

    return 0;
}

Which one does it call?

-[Unknown]


 struct A
 {
  int x, y;
 }
 
 void main()
 {
  static A a={5,10};
  A b={5,10};
 }
 
 The first line in main() is fine; the second isn't.
 
 Why not?  Structs are value types, yes?  So how is
 
 A b={5,10};
 
 any different from:
 
 int x=5;
 
 ?
 
 For that matter, how is this different than using an initializer:
 
 A b;
 b.x=5;
 b.y=10;
 
 I can't find any compelling reason for this to be illegal. 
 
 

Jun 27 2005
next sibling parent reply Chris Sauls <ibisbasenji gmail.com> writes:
Unknown W. Brackets wrote:
 void foobar(foo f)
 {
 }
 
 void foobar(bar b)
 {
 }
 
 int main()
 {
    foobar({0, 0});
 
    return 0;
 }
 
 Which one does it call?

And its a good question, and a fairly good reason why to make non-static initializers illegal; at least until one can find a solution. Already we have to use cast(char[])/cast(wchar[])/cast(dchar[]) with string literals to solve a similar issue, so maybe a cast(foo)/cast(bar) is in order? # foobar(cast(foo){0,0}); or maybe a 'new' expression would be a little more sensical? # foobar(new foo {0,0}); I'm fond of using 'new', except it currently would return a pointer. Maybe when new is paired with an initializer for structs, it could return a flat value instead of a pointer. That might be asking too much though. -- Chris Sauls
Jun 27 2005
parent reply Chris Sauls <ibisbasenji gmail.com> writes:
Chris Sauls wrote:
 # foobar(new foo {0,0});

I just thought I would re-emphasize this little off-the-top idea of mine, because the more I roll it around and play with it, the more I start to like it. Could the 'new' keyword be the latent solution to safe struct and array literals? Which brings me to one of my little questions about D that I never bothered asking, because it wasn't that important before... # struct Foo { ... } # Foo f = new Foo { ... } # # int[] arr = new int[] [ ... ] Why are []'s used instead of {}'s for array literals?? I've only ever known one other language to use []'s this way, and that was ColdC -- which used them for literals of the List and Dictionary types... and partly for the Frob literal. I never understood why ColdC uses them either, to be honest. -- Chris Sauls
Jun 28 2005
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Chris Sauls" <ibisbasenji gmail.com> wrote in message 
news:d9r2k8$odn$1 digitaldaemon.com...
 Why are []'s used instead of {}'s for array literals??  I've only ever 
 known one other language to use []'s this way, and that was ColdC -- which 
 used them for literals of the List and Dictionary types... and partly for 
 the Frob literal.  I never understood why ColdC uses them either, to be 
 honest.

Maybe to be more consistent. [] says "array" in any C-style language, and it makes sense to use [] to enclose array literals. Also, it might taks some of the overloading burden off of {}. The parser knows that [] can _only_ hold array elements, and can parse it as such.
Jun 28 2005
prev sibling next sibling parent AJG <AJG_member pathlink.com> writes:
What about using our friend Mr. Cast?

struct foo { int x, y; }
struct bar { int x, y; }
void foobar(foo f) {}
void foobar(bar b) {}

void main() {
foobar(cast(foo) {0, 0}); // OK: Calls foobar(foo f);
foobar(cast(bar) {0, 0}); // OK: Calls foobar(bar b);
foobar({0, 0}); // Error: Missing explicit cast.
}

One other possibility is for the compiler to try to "guess" which struct to cast
to; in case you don't want to specify it and if there's no ambiguity. But this
seems kinda hard to do, especially given implicit casting and whatnot.

Cheers,
--AJG.


In article <d9pma5$2ja5$1 digitaldaemon.com>, Unknown W. Brackets says...
This is why, to my understanding:

Which one does it call?

-[Unknown]


 struct A
 {
  int x, y;
 }
 
 void main()
 {
  static A a={5,10};
  A b={5,10};
 }
 
 The first line in main() is fine; the second isn't.
 
 Why not?  Structs are value types, yes?  So how is
 
 A b={5,10};
 
 any different from:
 
 int x=5;
 
 ?
 
 For that matter, how is this different than using an initializer:
 
 A b;
 b.x=5;
 b.y=10;
 
 I can't find any compelling reason for this to be illegal. 
 
 


Jun 27 2005
prev sibling next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Unknown W. Brackets" <unknown simplemachines.org> wrote in message 
news:d9pma5$2ja5$1 digitaldaemon.com...
 Which one does it call?

Ahh... and then this leads into the whole debate about struct ctors. Gotcha. Though, like the others have said, it could be casted.
Jun 27 2005
parent reply "Regan Heath" <regan netwin.co.nz> writes:
On Mon, 27 Jun 2005 21:09:43 -0400, Jarrett Billingsley  
<kb3ctd2 yahoo.com> wrote:
 "Unknown W. Brackets" <unknown simplemachines.org> wrote in message
 news:d9pma5$2ja5$1 digitaldaemon.com...
 Which one does it call?

Ahh... and then this leads into the whole debate about struct ctors. Gotcha. Though, like the others have said, it could be casted.

It would still be nice, even if casting wasn't implemented, to be able to say: A b = {5,10}; like we can in C. It was my impression we would get these, and further: A[10] b = [ {0,1},{1,2},{2,3},{3,4},{4,5}, {5,6},{6,7},{7,8},{8,9},{9,10} ]; basically, it's possible when it's not ambiguious. Regan
Jun 27 2005
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Regan Heath" <regan netwin.co.nz> wrote in message 
news:opss15u5cv23k2f5 nrage.netwin.co.nz...
 It would still be nice, even if casting wasn't implemented, to be able to 
 say:
 A b = {5,10};

Indeed. This topic actually came to my mind during the tedium of typing out: Vector v; v.x=5; v.y=10; v.z=15; v.w=1; Sure, you can use the static opCast() to fake a constructor, but then you're left with a performance vs. useability decision: do you make the static opCast() return an instance of the struct on the stack, possibly copying lots of data? Or do you return a pointer to a new struct, being faster, but meaning that you have to deal with pointer semantics? Having initializers makes it easier.
Jun 27 2005
prev sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Unknown W. Brackets wrote:
 This is why, to my understanding:
 
 struct foo
 {
    int x, y;
 }
 struct bar
 {
    int x, y;
 }
 
 void foobar(foo f)
 {
 }
 
 void foobar(bar b)
 {
 }
 
 int main()
 {
    foobar({0, 0});
 
    return 0;
 }
 
 Which one does it call?

That's not the meaning of the word "initializer". An initializer is something that follows the '=' in a declaration, as in the OP's code. This is syntactically, and to some extent semantically, distinct from an expression. Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Jun 30 2005
parent reply "Unknown W. Brackets" <unknown simplemachines.org> writes:
True, but he asked why and I answered; as far as I understand, that is 
why they are illegal.  Initializers themselves are not hampered by this 
problem, but likely it is waiting for a solution to the overall problem.

-[Unknown]


 That's not the meaning of the word "initializer".
 
 An initializer is something that follows the '=' in a declaration, as in 
 the OP's code.  This is syntactically, and to some extent semantically, 
 distinct from an expression.
 
 Stewart.
 

Jun 30 2005
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Unknown W. Brackets wrote:
 True, but he asked why and I answered; as far as I understand, that is 
 why they are illegal.

Your comment had exactly nothing to do with why the OP's code is invalid. static A a={5,10}; isn't ambiguous, so how can A b={5,10}; possibly be ambiguous?
 Initializers themselves are not hampered by this 
 problem, but likely it is waiting for a solution to the overall problem.

AIUI the only pending difficulty in adding struct/array literals is deciding on the syntax. If this is the case, there is nothing impeding the above notation (already _syntactically_ valid, and unambiguous) from being legalised. Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Jul 01 2005
next sibling parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Stewart Gordon wrote:

 AIUI the only pending difficulty in adding struct/array literals is 
 deciding on the syntax.  If this is the case, there is nothing impeding 
 the above notation (already _syntactically_ valid, and unambiguous) from 
 being legalised.

It should also be possible to decide on a valid D syntax, long before it is actually implemented ? As it is now, the language spec is rather closely tied to the reference compiler... Similar goes for other peeves, like the return value of main. Wouldn't hurt to have it at least "defined", even if not enforced or implemented by the current compilers. Like a todo list, or roadmap. And I wouldn't mind seeing a "looser coupling" between: - D, the language specication - D, the reference compiler (DMD) - D, the standard library (Phobos) I think it would also help alternative compilers like GDC, and alternative libraries like the one from the Ares project. But maybe this is something for "after the release", or so ? But it's somewhat annoying to not know what _year_ that will be... :-P --anders
Jul 01 2005
prev sibling parent reply "Unknown W. Brackets" <unknown simplemachines.org> writes:
But, as I said, why have two syntaxes?  Because you like Perl, or just 
because you want the langauge to be confusing?

-[Unknown]


 AIUI the only pending difficulty in adding struct/array literals is 
 deciding on the syntax.  If this is the case, there is nothing impeding 
 the above notation (already _syntactically_ valid, and unambiguous) from 
 being legalised.

Jul 01 2005
parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Unknown W. Brackets wrote:
 But, as I said, why have two syntaxes?

The struct initialiser syntax was obviously designed to look like that of C. However, it isn't suitable for general use in expressions because of the ambiguity you mentioned. But it's a neat-looking syntax nonetheless. And so Walter obviously saw fit to follow C's example by having two nonterminals, Initializer and Expression, each a superset of AssignExpression.
 Because you like Perl, or just because you want the langauge to be
 confusing?

What's confusing about it? Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Jul 04 2005
prev sibling parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Jarrett Billingsley wrote:

 I can't find any compelling reason for this to be illegal. 

I think it is just not implemented yet ? (unfortunately) --anders
Jun 29 2005