ozra / onyx-lang

The Onyx Programming Language
Other
97 stars 5 forks source link

Anon function self-reference syntax #96

Open Sod-Almighty opened 8 years ago

Sod-Almighty commented 8 years ago

It may be helpful to have a way to refer to the current function from within itself, without using its name - e.g. for anonymous functions. Consider:

the-factorial = (n)~>
  n == 1 ? n : n * ~(n+1)

puts the-factorial(5)

As an aside...how - using the current Onyx syntax - would I call the anon function in-place? In Ruby, I can invoke an anon function without assigning it to a variable:

->(params...){
   ...code...
}.(args...)

In Onyx, which is indent-based, it would seem I can't:

(params...)~>
 ...code...
.(args...)        -- nope!
ozra commented 8 years ago

The simplest way is without a doubt enclosing the lambda expression in parentheses: ((x Str) -> say x)("Yo anonymous!"), and then calling with parentheses syntax for the arguments.

Note that fragments do not need typed parameters, because they are invoked in a given context where the types can be inferred, and thus they can't be used stand alone (unless typed into a lambda).

Lambdas however, that can be used "stand alone", and passed around, need to have their arguments typed. This is because (unless you use closured variables in it) it is a C-compatible function (pointer), and because there's no telling where it will be passed.

Thus ((x) ~> say x)("Yo anonymous!") will not work:

can't execute `((x)~>
  $.raise("can't execute `say(x)` at /tmp/foo.ox:1:9: `x` has no type")
-- my-lambda is a neat and tidy super compatible first class function
my-lambda = (x Str) -> y = stuff x; do-more-stuff y
my-lambda "foo"

-- arg is _yielded_ to the fragment, and type inferred in the process:
some-fun-taking-fragment (x) ~> y = stuff x; do-more-stuff y  

It might be possible to implement type inference for when calling a lambda and/or fragment immediately, not sure how hard it would be, probably some crazy edge cases...

The "man-or-boy" compiler challenge (which exist only to terrorize compilers) shows that Onyx is a man-compiler, but, unfortunately self-reference is not possible in the lambda-body without declaring it before (b = raw () -> T) :-/

-- "man-or-boy.ox"
a(k T, x1, x2, x3, x4, x5) ->
   b = raw () -> T
   if k <= 0 ? x4() + x5() : (b = () -> k -= 1; a k, b, x1, x2, x3, x4)()

say a 10, () -> 1, () -> -1, () -> -1, () -> 1, () -> 0

I think it's quite a bit harder then it's worth. How big portion of your code is actually made out of such acrobatics?