Jump to page: 1 2
Thread overview
Why are non-static struct initializers illegal?
Jun 27, 2005
Chris Sauls
Jun 28, 2005
Chris Sauls
Jun 27, 2005
AJG
Jun 28, 2005
Regan Heath
Jun 30, 2005
Stewart Gordon
Jul 01, 2005
Stewart Gordon
Jul 04, 2005
Stewart Gordon
June 27, 2005
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.


June 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. 
> 
> 
June 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?

# 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
June 27, 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.
>> 
>> 


June 28, 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.


June 28, 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...
>> 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
June 28, 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.


June 28, 2005
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
June 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.


June 29, 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
« First   ‹ Prev
1 2