Project

General

Profile

Actions

Feature #16411

open

Safer keyword argument extension

Added by Dan0042 (Daniel DeLorme) almost 2 years ago. Updated almost 2 years ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:96172]

Description

The problem of safe keyword extension has been described like this:

#We have an existing method
def debug(*args)
  args.each {|arg| puts arg.inspect }
end

#It can be invoked using the "braceless hash style"
debug(key: 42) #=> {:key=>42}

#Then, consider we improve (extend) the method to accept the output IO as a keyword parameter "output":
def debug(*args, output: $stdout)
  args.each {|arg| output.puts arg.inspect }
end

#However, this change breaks the existing call.
debug(key: 42) #=> ArgumentError (unknown keyword: key)

The best solution to this is currently seen as

def debug(*args, output: $stdout, **hash)
  args << hash unless hash.empty?
  args.each {|arg| output.puts arg.inspect }
end
debug(key: 42) #=> {:key=>42}

With the caveat that it doesn't work if the braceless hash has a key in common with the new keyword argument(s):

debug(input: "a", output: "A") #=> NoMethodError (private method `puts' called for "A":String)

That's a reasonable compromise, but I think it would be very unusual to mix positional arguments and keyword arguments into a single braceless hash. So in the case above, if hash is non-empty, it's quite likely that the output key should be part of it, and not interpreted as a keyword argument. In short I'm saying that:

debug({input: "a"})                #is likely
debug(input: "a")                  #is likely
debug({input: "a"}, output:STDERR) #is likely
debug(input: "a", output:STDERR)   #is unlikely, or at least very bad form

So following from that, I believe the safest way to extend a method with keyword arguments is like this:

def debug(*args, **hash)
  defaults = {output: $stdout}
  kw = defaults.merge(hash)
  if kw.size > defaults.size #hash has some key not in defaults
    args << hash
    kw = defaults
  end
  output = kw[:output]
  args.each {|arg| output.puts arg.inspect }
end
debug(key: 42)                                   #=> {:key=>42}
debug({input: "a", output: "A"})                 #=> {:input=>"a", :output=>"A"}
debug(input: "a", output: "A")                   #=> {:input=>"a", :output=>"A"}
debug({input: "a", output: "A"}, output: STDERR) #=> {:input=>"a", :output=>"A"}

Of course it doesn't handle debug(output: "A") but I don't think there's any generic way to handle that.

The solution above is obviously way too verbose and way too much boilerplate to be used as-is, and that's why I'd like ruby to provide some kind of facility to perform this operation. The above could be somewhat simplified via some kind of utility function like

def debug(*args, **hash)
  kw = kw_or_hash(hash, output: $output){ |hash| args << hash }
  output = kw[:output]
  args.each {|arg| output.puts arg.inspect }
end

but ideally this safe keyword extension mechanism would allow to keep the keywords definition inside the method definition, unlike the above.
This could perhaps be done via some special syntax like

def debug(*args, output: $stdout, **!?)

but I don't think yet more special-case syntax is an ideal solution either.
Maybe the ideal would be via meta-programming like

extended_keywords def debug(*args, output: $stdout)

but I'm not sure how that would work internally. Is it possible?

Actions

Also available in: Atom PDF