apricot-lang / apricot

Clojure-like Lisp on Rubinius
94 stars 7 forks source link

Block Argument Passing #8

Closed solson closed 12 years ago

solson commented 12 years ago

We need to be able to pass blocks to Ruby methods that expect them from Apricot code. Block arguments are treated specially by Ruby and the following will not pass the fn as a block arg, but as a regular arg, which will fail:

(. [1 2 3] map (fn [x] (+ x 1))) ; ArgumentError: method 'map': given 1, expected 0

One solution would be to use a special character that the send expression (the .) would take to mean that the next argument should be passed as the block argument. For example, using the | character:

(. [1 2 3] map | (fn [x] (+ x 1))) ;=> [2 3 4]

For the most part, block argument passing should be hidden away under apricot wrappers. It should only be necessary for Ruby interop, so it need not be very pretty.

solson commented 12 years ago

Addition: since #2 is going to be implemented, we also need to consider what block arg passing looks like in combination with that syntax. But again, it doesn't have to be very pretty.

(. [1 2 3] map | #(+ % 1)) ;=> [2 3 4]

I think it doesn't look too bad.

solson commented 12 years ago

This is now implemented exactly as described here.