Nicer syntax for constructors

Erik van Velzen erik at evanv.nl
Mon Nov 19 21:36:32 UTC 2018


On Monday, 19 November 2018 at 12:30:20 UTC, NoMoreBugs wrote:
> On Sunday, 18 November 2018 at 19:09:16 UTC, Trailzz wrote:
>>
>> I'm not sure exactly what the syntax would be like, but I just 
>> wanted to know what other people think about this. Would it be 
>> worth creating a DIP for this?
>
>
> Well, let's step back a bit, from the so called 'code bloat', 
> and have a look at the definition of a function:
>
> return-type function-name(parameter declarations, if any)
> {
>   declarations
>   statements
> }
>
> What you are suggesting, is, in order to save a few keystrokes, 
> that D changes the definition of a function (or a constructor 
> function in this case).
>
> No language that has functions allows you do initialize 
> variables in the section where you declare your parameters - at 
> least, no language that I am aware of.
>

TypeScript:

class MyClass {
     constructor(
         readonly myValue: number,
         myArgument: number,
     ) {}
}

also Kotlin:

class MyClass(
     val myValue: Int,
     myArgument: String
) {}

> Lets not surprise newcomers to D, anymore than they are already 
> going to be surprised.
>
> Its bad enough you can't have a private member within a module!
>
> Initializing variables in the parameter section of the function 
> would be a step too far.


More information about the Digitalmars-d mailing list