3 ms·
That's already the case for any dynamic languages, and it's just down to whether you write your code to be flexible about inputs. E.g def int(i) = i.respon
by vidarh 3y ago
That's already the case for any dynamic languages, and it's just down to whether you write your code to be flexible about inputs. E.g
def int(i) = i.respond_to(:call) ? i.call : i.to_i
def f(i) = int(i) * 2
f in this case can take both ints and lambdas or anything call-able.
In practice we'd usually do this via any of the normal conversion methods like expecting the caller to support to_i etc., and bear the complexity for us as needed unless there's a really compelling reason to think the caller will often want to defer the evaluation past the call site and into the method itself.
That's usually very obvious when you come across it, like e.g. if the argument is expected to come from a remote source, but as long as the API for how the method uses the value is clear, substituting the value for a deferred calculation is easy.