dry-transformer icon indicating copy to clipboard operation
dry-transformer copied to clipboard

Composite#call should take a variable number of arguments

Open robhanlon22 opened this issue 3 years ago • 1 comments

Take this example:

# example.rb

require "dry/transformer"

module F
  extend Dry::Transformer::Registry

  def self.constantly(n)
    n
  end

  def self.square(n)
    n ** 2
  end
end

def F(*args)
  F[*args]
end

f = F(:constantly, 5) >> F(:square)
f.call
❯ ruby example.rb
Traceback (most recent call last):
	1: from example.rb:20:in `<main>'
/Users/me/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/dry-transformer-0.1.1/lib/dry/transformer/composite.rb:32:in `call': wrong number of arguments (given 0, expected 1) (ArgumentError)

Since all arguments have been applied, f.call should not require any arguments. However, Composite#call always requires one argument: https://github.com/dry-rb/dry-transformer/blob/master/lib/dry/transformer/composite.rb#L32 A simple fix would just to make Composite#call take a variable number of arguments, so that the first function in the chain can be called with as many arguments as it needs.

robhanlon22 avatar Sep 14 '20 00:09 robhanlon22

Do keep in mind *args is an extra allocation, so this potential fix would slow all other cases somewhat.

flash-gordon avatar Sep 14 '20 09:09 flash-gordon