hkust-taco / mlscript

The MLscript programming language. Functional and object-oriented; structurally typed and sound; with powerful type inference. Soon to have full interop with TypeScript!
https://hkust-taco.github.io/mlscript
MIT License
175 stars 27 forks source link

Iron out semantics of new object definition parameters and fields #166

Closed LPTK closed 1 year ago

LPTK commented 1 year ago

Definitions:

Implicit this insertion: Accesses to members in scope are implicitly prefixed with this. in JS Direct member access: Accesses to members in scope refer to the specific member definition does not virtually dispatch to possibly overridden ones. This might be a bit annoying to implement for methods.

Currently, we use direct access for parameters, which is (currently) not very consistent but has advantages:

Pros of direct accesses for methods and fields:

Cons of direct accesses for methods:

LPTK commented 1 year ago
LPTK commented 1 year ago

After some consideration, I figured the best approach may be to:

This has lots of benefits:

I expect virtual members to be the exception rather than the rules; therefore, the limitations attached to them are a fair price to pay for all these benefits.


(1) Consider how Scala is affected by potential initialization problems/footguns due to implicit virtuality by default (unless one uses the new initialization checker, which itself adds complexity and mental burden):

abstract class Foo { val x: Int; def foo = x }
object Bar extends Foo { val x = foo }
Bar.x // 0 (!!! uninitialized !!!)
class A { val x = 1; val y = x + 1; def z = x + 1 }
object B extends A { override val x = 2 }
B.y // 1 (!!! value was set from uninitialized access to x, yielding 0 !!!)
B.z // 3
LPTK commented 1 year ago

Addressed in https://github.com/hkust-taco/mlscript/pull/175.