From: Joshua Ballanco Date: 2011-10-27T04:03:52+09:00 Subject: [ruby-core:40418] Re: [ruby-trunk - Feature #5474][Assigned] keyword argument --20cf303bf65cfeb90a04b0385009 Content-Type: text/plain; charset=UTF-8 On Wed, Oct 26, 2011 at 2:08 PM, Yukihiro Matsumoto wrote: > Hi, > > In message "Re: [ruby-core:40414] Re: [ruby-trunk - Feature > #5474][Assigned] keyword argument" > on Thu, 27 Oct 2011 02:51:51 +0900, Charles Oliver Nutter < > headius@headius.com> writes: > > |Evan and I would like to see support for non-default keyword args. So > |the following form would require that "size" be provided as a keyword > |arg, and otherwise raise an ArgumentError: > | > |def create_point(x, y, color: "white", size:) > > I don't know the reason why you need this value-less keyword > argument. If you want the default value, you just need to omit the > keyword altogether. > I think the point is that not every argument necessarily has a reasonable default. Imagine, for example, if we were to rewrite File.open to take keyword arguments: class File def self.open(filename:, mode: 'r') ... It would be desirable to call such a method as `File.open(filename: "foo.txt")` but omitting the "filename:" argument should be an error. Perhaps, if a hash-like syntax is desired, we could introduce a "required" keyword: class File def self.open(filename: required, mode: 'r') ... such that omitting the option raises an ArgumentError? --20cf303bf65cfeb90a04b0385009 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On Wed, Oct 26, 2011 at 2:08 PM, Yukihiro Ma= tsumoto <matz@ru= by-lang.org> wrote:
Hi,

In message "Re: [ruby-core:40414] Re: [ruby-trunk - Feature #5474][Ass= igned] keyword argument"
=C2=A0 =C2=A0on Thu, 27 Oct 2011 02:51:51 +0900, Charles= Oliver Nutter <headius@headius.c= om> writes:

|Evan and I would like to see support for non-default keyword args. So
|the following form would require that "size" be provided as a ke= yword
|arg, and otherwise raise an ArgumentError:
|
|def create_point(x, y, color: "white", size:)

I don't know the reason why you need this value-less keyword
argument. =C2=A0If you want the default value, you just need to omit the keyword altogether.

I think the point i= s that not every argument necessarily has a reasonable default. Imagine, fo= r example, if we were to rewrite File.open to take keyword arguments:

=C2=A0 =C2=A0 class File
=C2=A0 =C2=A0 =C2=A0= def self.open(filename:, mode: 'r')
=C2=A0 =C2=A0 =C2=A0= ...

It would be desirable to call such a method a= s `File.open(filename: "foo.txt")` but omitting the "filenam= e:" argument should be an error. Perhaps, if a hash-like syntax is des= ired, we could introduce a "required" keyword:

=C2=A0 =C2=A0 class File
=C2=A0 =C2=A0 =C2=A0= def self.open(filename: required, mode: 'r')
=C2=A0 =C2= =A0 =C2=A0 ...

such that omitting the option raise= s an ArgumentError?=C2=A0
--20cf303bf65cfeb90a04b0385009--