digitalmars.D - Why are non-static struct initializers illegal?
- Jarrett Billingsley (20/20) Jun 27 2005 struct A
- Unknown W. Brackets (22/54) Jun 27 2005 This is why, to my understanding:
- Chris Sauls (12/28) Jun 27 2005 And its a good question, and a fairly good reason why to make non-static...
- Chris Sauls (15/16) Jun 28 2005 I just thought I would re-emphasize this little off-the-top idea of mine...
- Jarrett Billingsley (6/11) Jun 28 2005 Maybe to be more consistent. [] says "array" in any C-style language, a...
- AJG (16/51) Jun 27 2005 What about using our friend Mr. Cast?
- Jarrett Billingsley (5/6) Jun 27 2005 Ahh... and then this leads into the whole debate about struct ctors.
- Regan Heath (12/18) Jun 27 2005 It would still be nice, even if casting wasn't implemented, to be able t...
- Jarrett Billingsley (15/18) Jun 27 2005 Indeed. This topic actually came to my mind during the tedium of typing...
-
Stewart Gordon
(10/37)
Jun 30 2005
- Unknown W. Brackets (4/12) Jun 30 2005 True, but he asked why and I answered; as far as I understand, that is
- Stewart Gordon (15/19) Jul 01 2005 Your comment had exactly nothing to do with why the OP's code is invalid...
- =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= (16/20) Jul 01 2005 It should also be possible to decide on a valid D syntax,
- Unknown W. Brackets (3/7) Jul 01 2005 But, as I said, why have two syntaxes? Because you like Perl, or just
- Stewart Gordon (13/16) Jul 04 2005 The struct initialiser syntax was obviously designed to look like that
- =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= (3/4) Jun 29 2005 I think it is just not implemented yet ? (unfortunately)
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
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
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? or maybe a 'new' expression would be a little more sensical? 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
Chris Sauls wrote: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... 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
"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
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
"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
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...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. ReganWhich 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
"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
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?<snip top of upside-down reply> 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
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
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.<snip top of upside-down reply> 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
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
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
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?<snip top of upside-down reply> 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
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