DMD 0.177 release

Chad J gamerChad at _spamIsBad_gmail.com
Mon Dec 11 00:10:50 PST 2006


Walter Bright wrote:
> Burton Radons wrote:
> 
>> Here's the difference:
>>
>>     struct S
>>     {
>>         this (int x)
>>         {
>>             w = calculate_something (x);
>>         }
>>
>>         this (int x, int y)
>>         {
>>             this (x);
>>             z = calculate (y);
>>         }
>>     }
> 
> 
> struct S
> {
>     static S opCall(int x)
>     {
>     S result;
>     result.w = calculate_something(x);
>     return result;
>     }
> 
>     static S opCall(int x, int y)
>     {
>     auto result = S(x);
>     result.z = calculate(y);
>     return result;
>     }
> }
> 
> It's 3 more lines of code. The two styles almost completely overlap, and 
> since the latter is already in use, adding ctors just seems redundant.

OK here are the things that make me want constructors instead of static 
opCall:

It takes a lot longer to type.  You say only 3 more lines, but in that 
example 9 lines of constructor code becomes 12 lines of opCall code. 
The resulting code that is specific to these features is 25% fluff, and 
that includes the trivial curly braces.  Character wise it is worse.

The typing doesn't bug me as much as this though: what if the struct's 
name changes?  And what if the opCall is heavily overloaded when the 
name changes?  It's more unneeded code refactoring.  That is one reason 
D constructors are so cool, and it kinda sucks that structs don't have 
that too.

Also, static opCall is almost always used in the same way as a 
constructor.  I'd expect them to have the same syntax, but they don't. 
I think this, and some of the above reaons, result in the workaround 
perception - everyone expects the smooth 'this' syntax, but get static 
opCall instead.

Now I read that you would like to keep the semantic differences intact 
because they are useful.  Forcing the possibility of a bitwise copy of 
the struct before it is unleashed will apparently allow for cool stuff. 
  I don't think it's too unreasonable to have the 'this' identifier be a 
value rather than a reference in a struct constructor.  Thus you have a 
function that implicitly creates a blank instance of the struct, then 
allows the programmer to modify it via 'this', and implicitly returns 
the 'this' struct as static opCall would.



More information about the Digitalmars-d-announce mailing list