ozra / onyx-lang

The Onyx Programming Language
Other
97 stars 5 forks source link

User-defined operators #70

Closed stugol closed 8 years ago

stugol commented 8 years ago

I would like to be able to define my own operators:

inline ∋(&this : Enumerable)(other) ->
  this.includes? other

say [1, 2, 3] ∋ 1       -- true
unary prefix ++(&this : Int)() ->
  this += 1

unary suffix ++(&this : Int)() ->
  result = this
  this += 1
  result

n = 1
say n++        -- 1
say ++n        -- 3

And why stop there?

type MyNewArray:
   ...
literal [: :] (*args) ->
   MyNewArray(*args)

say [: 1, 2, 3 :]      -- same as MyNewArray(1, 2, 3)
literal 「 」(text) ->
   ...

say 「some text」      -- user-defined string literal - e.g. with some custom form of interpolation
stugol commented 8 years ago

Further:

type MyNewArray:
   constructor (*args) ->
      ...
   'literal [: :]       -- define a literal constructor

say [: 1, 2, 3 :]      -- same as MyNewArray(1, 2, 3)
ozra commented 8 years ago

I've been down this line back in the days. It's akin to Agda, or to the extreme: Katahdin (Ok, more common languages have them too, but these were some sources of inspiration when I was dabbling with it). Well, let's say, I have an open mind to user-defined operators, but I personally won't get my fingers dirty in that pot any time soon ;-) PR's are welcome.

ozra commented 8 years ago

I'm closing this. If a PR comes in, it's on the table.