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

Rudimentary support for constructors #27

Closed
TobiasWrigstad opened this issue Aug 8, 2014 · 7 comments
Closed

Rudimentary support for constructors #27

TobiasWrigstad opened this issue Aug 8, 2014 · 7 comments
Assignees

Comments

@TobiasWrigstad
Copy link
Contributor

Constructors are really tricky to get right, and there are many many issues such as leaking of this before an object has been fully initialised etc. This is clearly important for any PL project, but for the moment, I believe it is not so important to get it "right from the start".

I have written many lines like this:

let
  foo = let t = new Bar in { t.init(…) ; t }
  …
in
  …

and it makes me sad each time. For now, I propose that we give special status to methods named init in that we allow them to be called directly on object creation. I propose that we support the following syntactic sugar

new Bar(a,b,c) 

which is desugared into

let tmp = new Bar in { tmp.init(a,b,c) ; tmp }

This keeps init a normal method in all other respects, i.e., its return type can be void or anything, and it can be called at any point. A more general approach would be to allow something like

new Bar.method(a,b,c)

for some syntax like that to remove the special status of init, but we want to move in the direction of constructors in the future, so I'd rather have a very specific temporary feature than one which may actually be missed in the future.

TobiasWrigstad pushed a commit that referenced this issue Aug 8, 2014
@TobiasWrigstad
Copy link
Contributor Author

As of now, this feature is implemented. In part due to a lack of a typed desugaring step, the feature is lacking in at least two areas:

  • The call to init should be a future-less ! call if the class is active, but we have no way of knowing that in the desugaring phase
  • If no init method is found, error message shows up in code that programmer has not written

@EliasC is adding an additional desugaring step to run after typing in which we will be able to optimise the init call to use ! where appropriate. Until then, I am leaving this ticket open.

@TobiasWrigstad
Copy link
Contributor Author

@EliasC what is the status of this?

@EliasC
Copy link
Contributor

EliasC commented Dec 8, 2014

@TobiasWrigstad There is a typed desugaring step in the compiler (called Optimizer) but there is nothing of interest in it. If you want I could take over this ticket and try to fix your two bullets above.

@TobiasWrigstad
Copy link
Contributor Author

Please do but don't stress to finish. As long as there is clear ownership etc.

@EliasC EliasC self-assigned this Dec 8, 2014
@EliasC
Copy link
Contributor

EliasC commented Dec 8, 2014

As of 91c04b0, constructor calls are future-less (and work for passive classes as well). I'm closing this issue and opening a more general issue in #42 addressing the second bullet.

@EliasC EliasC closed this as completed Dec 8, 2014
@supercooldave
Copy link

It would be nice to see some text about constructors in the manual.

@EliasC
Copy link
Contributor

EliasC commented Dec 8, 2014

@supercooldave Added a bit under Active/Passive Classes and incorporated it in the examples

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

No branches or pull requests

3 participants