Open chronium opened 4 years ago
Great idea, I loved this stuff in Ruby.
One thing that seems a bit weird to me is the operator overloads.
So you'd use :+
to overload the +
operator, but use it as simply +
later?
In that case I think simply fn + (self, other)
would make more sense.
The parser can easily handle that special case after fn
so you don't have to allow it for all identifiers at once.
Fair enough with operators being simple, instead of symbols. But maybe we could allow symbol function names for manual dynamic dispatch for example.
Another thing, what would we do for postfix operators? With the current design ideas, prefix functions are any function that has a single parameter. Infix functions are basically any function that takes two parameters, but what about postfix? Maybe we could use symbol function names for postfix?
Example:
fn foo_self (self) // names can be symbols and literal symbols from now on as well
@prec(22) // specify precedence level
fn foo_inf (self, other)
fn :? (self) // defines postfix `?`
// usage
foo_self self_inst
self_inst.foo_self
self_inst foo_inf other_inst
self_inst.foo_inf other_inst
self_inst.foo_inf(other_inst)
self_inst? // calls postfix `?` on self
If :?
denotes postfix, how would you do prefix operators?
Would it be ?:
then for ?self_inst
?
Maybe you could use the :
symbol to differentiate operator placement.
fn ? (self, true, false)
: foo.? (a, b)
fn ?: self
: ?foo
fn :? self
: foo?
In the current design, prefix is any function that takes a single argument.
Add symbol literals for use with quoting and macro creation, dictionary keys and other constructs. Symbols are primitive values, and can be composed. They would also be useful for defining custom operators. For example, overloading the
+
operator could be the form of:fn :+ (self, other)
Symbol literals would have the form of:
:[\S]+
Example
Given:
Extract the value: