Feature request - simpler constructors

Janice Caron caron800 at googlemail.com
Thu Sep 20 21:57:01 PDT 2007


On 9/21/07, Walter Bright <newshound1 at digitalmars.com> wrote:
> Janice Caron wrote:
> > Please could we let
>
> I think it's a good idea, but I think using the function signature for
> it probably is not the best idea (as the function signature is for the
> interface, not the internals).

Ah, well since that initial idea, lots of people have come up with
different alternatives, and the one suggested by Regan Heath is really
nice:

class Point {
  int x;
  int y;
  int foo;  //not implicitly assigned any value

  this(int x, int y, int bar):(x,y)  //bar is not assigned to anything
  {
    //super constructor calls go here
    //members are assigned here (_after_ calling super)
    //first line of user code goes here
    if (x > 5) {}  //refers to member 'x' not parameter 'x'
  }
}

It's sort of similar to C++'s approach, but less typing and you don't
have to pick two different names for each variable, and if you wrote
the colon bit on the next line, it wouldn't be in the function
signature. (And if you didn't like the overuse of colon, you could
always use a keyword like you do for in, out and body).

What I like about it is that you can do this:

  this(int x, int y, int bar):(y,x)  //first assign y, then assign x

in case there are side-effects to assignment order. Even better would
be to allow

  this(int x, int y, int bar):(...)

to be equivalent to

  this(int x, int y, int bar):(x,y,bar)

as between them, that set of possibilities seems to cover every need
suggested so far, including your need of not using the function
signature.



More information about the Digitalmars-d mailing list