ozra / onyx-lang

The Onyx Programming Language
Other
97 stars 5 forks source link

Request: Implicit receiver in blocks #65

Closed stugol closed 8 years ago

stugol commented 8 years ago

Consider the following Ruby example:

values.select { |v| v > 10 }.map { |v| (v * 2).to_s }

Wouldn't it be better phrased like this?

values.select { .> 10 }.map { (.*2).to_s }

In other words, for any given call x.y, we can omit the x, whereupon the first argument to the block is inserted.

Alternatively, have implicit block arguments:

values.select { @1 > 10 }.map { (@1 * 2).to_s }

The block is assumed to have sufficient arguments. For example, a block containing a reference to @4 is assumed to have the signature |@1,@2,@3,@4|. Alternatively, we could use % or &.

ozra commented 8 years ago

Cough, cough: #14 - all in place :-)

Fast recap (but check #14 out, consider it docs)

say [1, 12, 14].select(~.> 10).map(~.* 2).map(~.to-s)

say [1, 12, 14].select(~> _1 > 10).map(~> (_1 * 2).to-s)

-- or if you prefer

say [1, 12, 14]
.select ~> _1 > 10
.map ~> (_1 * 2).to-s

As you see the only gotcha for the ~. notation is that no parentheses can go before it (resulting in a additional map in the first case). I'll look into if I can improve that without syntax-clashes.

The last example would be nicer if it worked with indentation, probably should add lenience for that.

Your suggested @n instead of _n might be better also. Of course %n would parallel "fresh var" in macros (provided it turns out to be impossible to re-work).

Hmm, this reminded me of another idea - I'll put that in #21.

stugol commented 8 years ago

I would prefer @n or %n. _n is just....ugly.

Yes, indentation would be nice.

ozra commented 8 years ago

Looking at it I feel your auto-parametrization naming suggestions are a definite go. Don't know which one just yet. Will be changed as soon as that is clear.

stugol commented 8 years ago

I think @ makes the most sense.

ozra commented 8 years ago

I like the %n style, so, some alternatives:

One solution don't necessarily eliminate also another.

stugol commented 8 years ago

Require macro delimiters to be spaced from content

This. We need to enforce some spacing in the language, else we'll never get anything done. See #57.

Also, while %var is often used for "fresh variables" in macros, they are never numeric, so there is no conflict.

ozra commented 8 years ago

%n variation implemented. The prior syntax, _n, will be kept for a while for real life comparison, but will likely be dropped soon.