ponylang / rfcs

RFCs for changes to Pony
https://ponylang.io/
60 stars 48 forks source link

Syntax sugar to assigning a constructor argument to an instance variable and to add the keyword this (or self) #187

Open igotfr opened 3 years ago

igotfr commented 3 years ago

Alternative 1, like Dart:

class Point
  var x: F64
  var y: F64

  new create(this.x, this.y)

Alternative 2, like Python, Typescript

class Point
  new create(var this.x: F64, var this.y: F64)
malthe commented 3 years ago

See also https://pony.groups.io/g/user/message/1108.

jemc commented 3 years ago

In the Savi compiler I have adopted this approach, with the small change that it uses the @ prefix to designate the concept of this. (everywhere it appears - not just in this particular feature).

:class Point
  :var x F64
  :var y F64

  :new create(@x, @y)

So I endorse the "Alternative 1" approach above :+1:, though I'd also like to make the this. syntax be less verbose (probably in a separate RFC so as not to bog this one down in that controversy).

I'm not a fan of "Alternative 2" because it would make it more difficult to find the full list of the type's fields - especially if there are multiple constructors involved and not all of the fields appear as parameters in every constructor's signature.

jasoncarr0 commented 3 years ago

Kotlin and Scala are additional examples which have adopted a feature like this, specifically closer to alternative 2 (and for Kotlin there is the primary constructor mechanism which makes this the norm and follows the presented constructor version)