Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
igotfr opened this issue Jun 6, 2021 · 3 comments

Comments

@igotfr
Copy link

igotfr commented Jun 6, 2021

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
Copy link
Contributor

malthe commented Jun 7, 2021

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

@jemc
Copy link
Member

jemc commented Jun 7, 2021

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 👍,
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
Copy link

jasoncarr0 commented Jun 8, 2021

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants