Jump to page: 1 2 3
Thread overview
Nicer syntax for constructors
Nov 18, 2018
Trailzz
Nov 18, 2018
12345swordy
Nov 18, 2018
Trailzz
Nov 18, 2018
Jacob Carlborg
Nov 18, 2018
Trailzz
Nov 18, 2018
Timon Gehr
Nov 18, 2018
Trailzz
Nov 18, 2018
Timon Gehr
Nov 19, 2018
dayllenger
Nov 20, 2018
aberba
Nov 18, 2018
Neia Neutuladh
Nov 18, 2018
Adam D. Ruppe
Nov 19, 2018
bauss
Nov 19, 2018
Adam D. Ruppe
Nov 19, 2018
FeepingCreature
Nov 19, 2018
NoMoreBugs
Nov 19, 2018
Erik van Velzen
Nov 20, 2018
NoMoreBugs
Nov 24, 2018
JN
November 18, 2018
This situation happens *very* often in object oriented programming:
```
this(string a, int b, char c, float d, uint e, bool f)
{
    this.a = a;
    this.b = b;
    this.c = c;
    this.d = d;
    this.e = e;
    this.f = f;
}
```

It would save a lot of code bloat if there was a simpler syntax for this, perhaps something like this:
```
this(string this.a, int this.b, char this.c, float this.d, uint this.e, bool this.f)
{
}
```

It would be very easy to infer the types here, so this could also work
```
this(this.a, this.b, this.c, this.d, this.e, this.f)
{
}
```

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?
November 18, 2018
On Sunday, 18 November 2018 at 19:09:16 UTC, Trailzz wrote:
> This situation happens *very* often in object oriented programming:
> ```
> this(string a, int b, char c, float d, uint e, bool f)
> {
>     this.a = a;
>     this.b = b;
>     this.c = c;
>     this.d = d;
>     this.e = e;
>     this.f = f;
> }
> ```
>
> It would save a lot of code bloat if there was a simpler syntax for this, perhaps something like this:
> ```
> this(string this.a, int this.b, char this.c, float this.d, uint this.e, bool this.f)
> {
> }
> ```
>
> It would be very easy to infer the types here, so this could also work
> ```
> this(this.a, this.b, this.c, this.d, this.e, this.f)
> {
> }
> ```
>
> 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?

Have tried to use templates/string mixin for this? Don't rush to a DIP yet, until you exhaust other options.
November 18, 2018
On Sunday, 18 November 2018 at 19:25:09 UTC, 12345swordy wrote:
> Have tried to use templates/string mixin for this? Don't rush to a DIP yet, until you exhaust other options.

how would you use templates/string mixin for this?
November 18, 2018
On Sun, 18 Nov 2018 19:09:16 +0000, Trailzz wrote:
> It would be very easy to infer the types here, so this could also work
> ```
> this(this.a, this.b, this.c, this.d, this.e, this.f)
> ```

Dart does this, for reference. Some random code I happened to have lying around:

    class Stat {
      StatType type;
      ShipToValue shipToValue;
      Stat(this.type, this.shipToValue);
    }


So does CoffeeScript:

    class Animal
      constructor: (@name) ->

C++ has initializer lists, which are clunkier, and I think they avoid invoking a copy constructor or something.
November 18, 2018
On 2018-11-18 20:26, Trailzz wrote:
> On Sunday, 18 November 2018 at 19:25:09 UTC, 12345swordy wrote:
>> Have tried to use templates/string mixin for this? Don't rush to a DIP yet, until you exhaust other options.
> 
> how would you use templates/string mixin for this?

Something like this:

import std.traits : ParameterIdentifierTuple;

class Foo
{
    int a;
    int b;

    this(int a, int b)
    {
        static foreach (name ; ParameterIdentifierTuple!(__ctor))
            __traits(getMember, this, name) = mixin(name);
    }
}

void main()
{
    auto f = new Foo(2, 3);
    assert(f.a == 2);
    assert(f.b == 3);
}

-- 
/Jacob Carlborg
November 18, 2018
On Sunday, 18 November 2018 at 19:47:26 UTC, Jacob Carlborg wrote:
>     this(int a, int b)
>     {
>         static foreach (name ; ParameterIdentifierTuple!(__ctor))
>             __traits(getMember, this, name) = mixin(name);
>     }
> }

This is still very clunky and hard to understand.
November 18, 2018
On 18.11.18 20:52, Trailzz wrote:
> On Sunday, 18 November 2018 at 19:47:26 UTC, Jacob Carlborg wrote:
>>     this(int a, int b)
>>     {
>>         static foreach (name ; ParameterIdentifierTuple!(__ctor))
>>             __traits(getMember, this, name) = mixin(name);
>>     }
>> }
> 
> This is still very clunky and hard to understand.

---
module util;
enum assignFields=q{{
    import std.traits;
    static foreach(x;ParameterIdentifierTuple!(__traits(parent,{})))
        static if(__traits(hasMember, this, x))
            __traits(getMember,this,x)=mixin(x);
}};
---
---
module app;
import util;

class C{
    string a;
    int b;
    char c;
    float d;
    uint e;
    bool f;
    this(string a, int b, char c, float d, uint e, bool f){
        mixin(assignFields);
    }
}

void main(){
    import std.stdio;
    writeln((new C("1",2,'3',4.0,5,true).tupleof));
}
---
November 18, 2018
On Sunday, 18 November 2018 at 19:59:27 UTC, Timon Gehr wrote:
> On 18.11.18 20:52, Trailzz wrote:
>> On Sunday, 18 November 2018 at 19:47:26 UTC, Jacob Carlborg wrote:
>>> [...]
>> 
>> This is still very clunky and hard to understand.
>
> ---
> module util;
> enum assignFields=q{{
>     import std.traits;
>     static foreach(x;ParameterIdentifierTuple!(__traits(parent,{})))
>         static if(__traits(hasMember, this, x))
>             __traits(getMember,this,x)=mixin(x);
> }};
> ---
> ---
> module app;
> import util;
>
> class C{
>     string a;
>     int b;
>     char c;
>     float d;
>     uint e;
>     bool f;
>     this(string a, int b, char c, float d, uint e, bool f){
>         mixin(assignFields);
>     }
> }
>
> void main(){
>     import std.stdio;
>     writeln((new C("1",2,'3',4.0,5,true).tupleof));
> }
> ---

This is arguably more clunky than just lots of `this.foo = foo`.
November 18, 2018
class A {
  int a;
  string b;
  float c;

  this(typeof(this.tupleof) args) {
    this.tupleof = args;
  }
}


auto a = new A(4, "foo", 1.5);


Of course, that assumes none of the internal types are const, that complicates things, and it assumes you want to set them all at once. It also doesn't work quite right with inheritance, but you can make that work by combining the base class tuple if you want.

Keep in mind you can also slice tuples if you want a subset of the members.
November 18, 2018
On 18.11.18 21:06, Trailzz wrote:
> ...
> 
> This is arguably more clunky than just lots of `this.foo = foo`.

?

Define clunky.
« First   ‹ Prev
1 2 3