ozra / onyx-lang

The Onyx Programming Language
Other
95 stars 5 forks source link

Specific keyword for re-opening types #86

Closed ozra closed 8 years ago

ozra commented 8 years ago

This is something that costs very little effort, but can save a lot of unnecessary debugging time.

Now:

type Foo
   do-stuff() -> 47

type Foo
   more-stuff() -> 42

Proposed

type Foo
   do-stuff() -> 47

reopen Foo
   more-stuff() -> 42

The reopen is not considered the "to be" keyword. Suggestions are welcome. The important part is:

The natural continuation of this is of course member-funcs, which has been thought of for a while. I'm not to keen about requiring 'redef pragma for all overrides though. I think this should be only for funcs defined as overriding-is-bad (there is no final, just a suggestion, and 'redef 'force or something enables overriding anyway. Requiring `'redef' for all overrides (in order to ensure there actually is a method named as such that is overridden, and ensuring that a func is not named clashing with an existant unintentionally) should be an option through the idea of "code modes", and not standard.

stugol commented 8 years ago

Actually, I kinda like redef. We could go further, and make it easier to override a method:

type T ->
  redef x(arg) ->
    original(arg) + 1

Expands to:

class T
  original_x = instance_method(:x)
  def x(arg)
    original_x.bind(self).(arg) + 1
  end
end
ozra commented 8 years ago

original is currently available through previous-def, or super if an inherited overridden. overridden might be better for previous-def. It is however one of those quirky-to-spell-for-non-en-mother-tongued "keywords".

I still feel redef should be more of an annotation than keyword. Func-names should have their primary spatial position. There are arguments for both positions of course. In any event, then it would look like this:

type Typ
   x(arg) -> arg + 1

reopen Typ
   x(arg) -> 'redef
      overridden(arg) + 1
stugol commented 8 years ago

Hmm. I'd forgotten that we don't use def... ;)

ozra commented 8 years ago

reop, reopen, etc. are left out in favour of ext. Short, clean-sounding and correct english semantics (the full term). The full term extend will also be allowed - it's a style choice. I will personally prefer using the abbreviation.

The construct extend Something in Crystal will be include Something on Self in Onyx (only one use-case in the entire compiler source, the lengthier syntax is more than OK imo)

type Typ
   x(arg) -> arg + 1

ext Typ
   x(arg) -> 'redef
      overridden(arg) + 1
   a-new-func() -> true
ozra commented 8 years ago

This is a hit. Further in #18.