Project

General

Profile

Actions

Feature #7701

closed

Non-optional (required) keyword args

Added by headius (Charles Nutter) about 11 years ago. Updated over 2 years ago.

Status:
Closed
Target version:
-
[ruby-core:51454]

Description

=begin
I would like to see keyword args expanded to include a non-optional form, to force callers to pass in keyword arguments.

Currently, we have required, optional, and rest positional args but only optional and rest keyword args. Consistency is one small reason to add required keyword args.

They would likely take the form of keyword with no default value:

def foo(a:, b:)
...
end

foo(a: 1, b: 2) # ok
foo(a: 1) # ArgumentError

Justifications:

  • Consistency with positional args. A weak justification, I know.

  • Avoiding a lot of boilerplate code by users wishing to enforce keywords being passed in. Example from tenderlove:

    def foo(a: raise('pass a'), b: raise('pass b'))

  • Building a rich API atop keyword args would be easier (i.e. require fewer manual checks) if you could force some keywords to be passed in. Having to check everywhere when you require a keyword argument is unpleasant.

  • Keyword args already enforces that no additional keyword args can be passed (without **), and it seems lopsided to have no way to enforce a minimum set of keyword args.
    =end


Files

0001-required-keyword-arguments.patch (7.82 KB) 0001-required-keyword-arguments.patch nobu (Nobuyoshi Nakada), 02/16/2013 01:18 AM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0