Currying and block arity

Brian T. Rice water at tunes.org
Thu Jun 15 10:39:52 PDT 2006


On Thu, June 15, 2006 9:53 am, Timmy Douglas said:
>
> I was checking something out and I thought this looked a little weird:
>
> Slate 6> #+ `er <- 1.
> [(arity: 0)]
> Slate 7> #+ `er.
> [(arity: 2)]
>
> Slate 17> [].
> [(arity: 0)]
>
> how did the arity of the block jump by 2 when I only curried one
> thing? It still works of course when I applyWith: 1...

If you look at the definition of #fill:with: (which underlies currying) in
src/lib/method.slate, you'll see what the new method is:

m@(Method traits) fill: arg with: val
"Answer a new method based on the given one, with the argument at a given
index
filled in with a value, essentially currying the method."
[
  (arg between: 0 and:
    (m acceptsAdditionalArguments ifTrue: [PositiveInfinity] ifFalse: [m
arity - 1]))
    ifFalse: [error: 'Attempted to fill nonexistent method argument.'].
  [| *args | m applyTo: (args copyWith: val at: arg)]
].

So you're getting a wrapper method that requires zero inputs but takes all
inputs it is passed and applies them with the curried value to the
original.

This is not very efficient, of course, and invites improvement. The
ByteCompiler has code for "inlining" #fill:with: and #<- calls (see
src/mobius/vm/interp/compiler.slate), but they only work when there is a
block literal on the left, and in this case macro-expansion is too "late"
for this to be triggered.

-- 
http://tunes.org/~water/brice.vcf



More information about the Slate mailing list