ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:103305] [Ruby master Feature#17785] Allow named parameters to be keywords
@ 2021-04-08 15:08 marcandre-ruby-core
  2021-04-08 15:21 ` [ruby-core:103307] " mame
                   ` (18 more replies)
  0 siblings, 19 replies; 21+ messages in thread
From: marcandre-ruby-core @ 2021-04-08 15:08 UTC (permalink / raw)
  To: ruby-core

Issue #17785 has been reported by marcandre (Marc-Andre Lafortune).

----------------------------------------
Feature #17785: Allow named parameters to be keywords
https://bugs.ruby-lang.org/issues/17785

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
We should allow named parameters to be keywords and use add a trailing `_` to the corresponding variable:

```ruby
def check(arg, class:)
  arg.is_a?(class_)
end

check(42, class: Integer) # => true
```

Currently, if we want such an API we have to use `**rest`:

```ruby
def check(arg, **rest)
  class_ = rest.fetch(:class) { raise ArgumentError('missing keyword: :class')}
  if rest.size > 1
    unknown = rest.keys - [:class]
    raise ArgumentError("unknown keyword(s): :#{unknown.join(', :')})
  end

  arg.is_a?(class_)
end
```

This is very verbose, much less convenient, much less readable, prevents `steep` from generating the proper signature, etc.

We should do the same for pattern match.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [ruby-core:103307] [Ruby master Feature#17785] Allow named parameters to be keywords
  2021-04-08 15:08 [ruby-core:103305] [Ruby master Feature#17785] Allow named parameters to be keywords marcandre-ruby-core
@ 2021-04-08 15:21 ` mame
  2021-04-08 15:23 ` [ruby-core:103308] " zverok.offline
                   ` (17 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: mame @ 2021-04-08 15:21 UTC (permalink / raw)
  To: ruby-core

Issue #17785 has been updated by mame (Yusuke Endoh).


An interesting idea. I have never thought of it. A clearer name might be better, such as `keyword_variable_class`, instead of `class_`.

FYI, `Binding#local_variable_get` was introduced for the very use case.

```
def check(arg, class:)
  class_ = binding.local_variable_get(:class)
  arg.is_a?(class_)
end
```

Personally I don't like `local_variable_get`, though, because it is unreasonably slow, and still less convenient.

----------------------------------------
Feature #17785: Allow named parameters to be keywords
https://bugs.ruby-lang.org/issues/17785#change-91392

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
We should allow named parameters to be keywords and use add a trailing `_` to the corresponding variable:

```ruby
def check(arg, class:)
  arg.is_a?(class_)
end

check(42, class: Integer) # => true
```

Currently, if we want such an API we have to use `**rest`:

```ruby
def check(arg, **rest)
  class_ = rest.fetch(:class) { raise ArgumentError('missing keyword: :class')}
  if rest.size > 1
    unknown = rest.keys - [:class]
    raise ArgumentError("unknown keyword(s): :#{unknown.join(', :')})
  end

  arg.is_a?(class_)
end
```

This is very verbose, much less convenient, much less readable, prevents `steep` from generating the proper signature, etc.

We should do the same for pattern match.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [ruby-core:103308] [Ruby master Feature#17785] Allow named parameters to be keywords
  2021-04-08 15:08 [ruby-core:103305] [Ruby master Feature#17785] Allow named parameters to be keywords marcandre-ruby-core
  2021-04-08 15:21 ` [ruby-core:103307] " mame
@ 2021-04-08 15:23 ` zverok.offline
  2021-04-08 16:55 ` [ruby-core:103312] " marcandre-ruby-core
                   ` (16 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: zverok.offline @ 2021-04-08 15:23 UTC (permalink / raw)
  To: ruby-core

Issue #17785 has been updated by zverok (Victor Shepelev).


We actually can:
```ruby
def check(arg, class:)
  arg.is_a?(binding.local_variable_get('class'))
end
```
[Here](https://bugs.ruby-lang.org/issues/15647#change-77000) @nobu have argued that this is exactly how it is intended to be done.

I vaguely remember arguing somewhere about some syntax to access "specially named" local vars, but I don't remember what I've proposed then, and can't find the ticket :shrug:

----------------------------------------
Feature #17785: Allow named parameters to be keywords
https://bugs.ruby-lang.org/issues/17785#change-91393

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
We should allow named parameters to be keywords and use add a trailing `_` to the corresponding variable:

```ruby
def check(arg, class:)
  arg.is_a?(class_)
end

check(42, class: Integer) # => true
```

Currently, if we want such an API we have to use `**rest`:

```ruby
def check(arg, **rest)
  class_ = rest.fetch(:class) { raise ArgumentError('missing keyword: :class')}
  if rest.size > 1
    unknown = rest.keys - [:class]
    raise ArgumentError("unknown keyword(s): :#{unknown.join(', :')})
  end

  arg.is_a?(class_)
end
```

This is very verbose, much less convenient, much less readable, prevents `steep` from generating the proper signature, etc.

We should do the same for pattern match.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [ruby-core:103312] [Ruby master Feature#17785] Allow named parameters to be keywords
  2021-04-08 15:08 [ruby-core:103305] [Ruby master Feature#17785] Allow named parameters to be keywords marcandre-ruby-core
  2021-04-08 15:21 ` [ruby-core:103307] " mame
  2021-04-08 15:23 ` [ruby-core:103308] " zverok.offline
@ 2021-04-08 16:55 ` marcandre-ruby-core
  2021-04-09 13:04 ` [ruby-core:103346] " jean.boussier
                   ` (15 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: marcandre-ruby-core @ 2021-04-08 16:55 UTC (permalink / raw)
  To: ruby-core

Issue #17785 has been updated by marcandre (Marc-Andre Lafortune).


Clearly, `class_` is much simpler and much faster than `binding.local_variable_get(:class)`...

----------------------------------------
Feature #17785: Allow named parameters to be keywords
https://bugs.ruby-lang.org/issues/17785#change-91396

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
We should allow named parameters to be keywords and use add a trailing `_` to the corresponding variable:

```ruby
def check(arg, class:)
  arg.is_a?(class_)
end

check(42, class: Integer) # => true
```

Currently, if we want such an API we have to use `**rest`:

```ruby
def check(arg, **rest)
  class_ = rest.fetch(:class) { raise ArgumentError('missing keyword: :class')}
  if rest.size > 1
    unknown = rest.keys - [:class]
    raise ArgumentError("unknown keyword(s): :#{unknown.join(', :')})
  end

  arg.is_a?(class_)
end
```

This is very verbose, much less convenient, much less readable, prevents `steep` from generating the proper signature, etc.

We should do the same for pattern match.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [ruby-core:103346] [Ruby master Feature#17785] Allow named parameters to be keywords
  2021-04-08 15:08 [ruby-core:103305] [Ruby master Feature#17785] Allow named parameters to be keywords marcandre-ruby-core
                   ` (2 preceding siblings ...)
  2021-04-08 16:55 ` [ruby-core:103312] " marcandre-ruby-core
@ 2021-04-09 13:04 ` jean.boussier
  2021-04-09 17:59 ` [ruby-core:103350] " eregontp
                   ` (14 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: jean.boussier @ 2021-04-09 13:04 UTC (permalink / raw)
  To: ruby-core

Issue #17785 has been updated by byroot (Jean Boussier).


Arguably it's a bit of a stretch, but how would you handle: `foo(class_, class:)`?

What if instead of mangling the variable name, there would be a way to tell the parser to interpret the next word as a regular name rather than a keyword? e.g.:

```ruby
def check(arg, class:)
  arg.is_a?(\class)
end
```

`\` being a common escaping character I think it the one that would make the most sense.

And that would allow to make it work with regular parameters as well:

```ruby
def diff(start, \end)
  \end - start
end
```

Even though this use case is much less important, except for documentation purposes.

----------------------------------------
Feature #17785: Allow named parameters to be keywords
https://bugs.ruby-lang.org/issues/17785#change-91436

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
We should allow named parameters to be keywords and use add a trailing `_` to the corresponding variable:

```ruby
def check(arg, class:)
  arg.is_a?(class_)
end

check(42, class: Integer) # => true
```

Currently, if we want such an API we have to use `**rest`:

```ruby
def check(arg, **rest)
  class_ = rest.fetch(:class) { raise ArgumentError('missing keyword: :class')}
  if rest.size > 1
    unknown = rest.keys - [:class]
    raise ArgumentError("unknown keyword(s): :#{unknown.join(', :')})
  end

  arg.is_a?(class_)
end
```

This is very verbose, much less convenient, much less readable, prevents `steep` from generating the proper signature, etc.

We should do the same for pattern match.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [ruby-core:103350] [Ruby master Feature#17785] Allow named parameters to be keywords
  2021-04-08 15:08 [ruby-core:103305] [Ruby master Feature#17785] Allow named parameters to be keywords marcandre-ruby-core
                   ` (3 preceding siblings ...)
  2021-04-09 13:04 ` [ruby-core:103346] " jean.boussier
@ 2021-04-09 17:59 ` eregontp
  2021-04-09 20:57   ` [ruby-core:103354] " Austin Ziegler
  2021-04-10  0:06 ` [ruby-core:103360] " duerst
                   ` (13 subsequent siblings)
  18 siblings, 1 reply; 21+ messages in thread
From: eregontp @ 2021-04-09 17:59 UTC (permalink / raw)
  To: ruby-core

Issue #17785 has been updated by Eregon (Benoit Daloze).


I like @byroot's idea to solve the more general issue and not just this specific instance.

marcandre (Marc-Andre Lafortune) wrote in #note-3:
> Clearly, `class_` is much simpler and much faster than `binding.local_variable_get(:class)`...

It can be the same performance with a JIT and escape analysis (i.e., it's the same on TruffleRuby).

----------------------------------------
Feature #17785: Allow named parameters to be keywords
https://bugs.ruby-lang.org/issues/17785#change-91441

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
We should allow named parameters to be keywords and use add a trailing `_` to the corresponding variable:

```ruby
def check(arg, class:)
  arg.is_a?(class_)
end

check(42, class: Integer) # => true
```

Currently, if we want such an API we have to use `**rest`:

```ruby
def check(arg, **rest)
  class_ = rest.fetch(:class) { raise ArgumentError('missing keyword: :class')}
  if rest.size > 1
    unknown = rest.keys - [:class]
    raise ArgumentError("unknown keyword(s): :#{unknown.join(', :')})
  end

  arg.is_a?(class_)
end
```

This is very verbose, much less convenient, much less readable, prevents `steep` from generating the proper signature, etc.

We should do the same for pattern match.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [ruby-core:103354] Re: [Ruby master Feature#17785] Allow named parameters to be keywords
  2021-04-09 17:59 ` [ruby-core:103350] " eregontp
@ 2021-04-09 20:57   ` Austin Ziegler
  0 siblings, 0 replies; 21+ messages in thread
From: Austin Ziegler @ 2021-04-09 20:57 UTC (permalink / raw)
  To: Ruby developers

I’ll also say I like byroot’s idea, especially as bare `\VALUE`
currently throws a SyntaxError.

On Fri, Apr 9, 2021 at 1:59 PM <eregontp@gmail.com> wrote:
>
> Issue #17785 has been updated by Eregon (Benoit Daloze).
>
>
> I like @byroot's idea to solve the more general issue and not just this specific instance.
>
> marcandre (Marc-Andre Lafortune) wrote in #note-3:
> > Clearly, `class_` is much simpler and much faster than `binding.local_variable_get(:class)`...
>
> It can be the same performance with a JIT and escape analysis (i.e., it's the same on TruffleRuby).
>
> ----------------------------------------
> Feature #17785: Allow named parameters to be keywords
> https://bugs.ruby-lang.org/issues/17785#change-91441
>
> * Author: marcandre (Marc-Andre Lafortune)
> * Status: Open
> * Priority: Normal
> * Assignee: matz (Yukihiro Matsumoto)
> ----------------------------------------
> We should allow named parameters to be keywords and use add a trailing `_` to the corresponding variable:
>
> ```ruby
> def check(arg, class:)
>   arg.is_a?(class_)
> end
>
> check(42, class: Integer) # => true
> ```
>
> Currently, if we want such an API we have to use `**rest`:
>
> ```ruby
> def check(arg, **rest)
>   class_ = rest.fetch(:class) { raise ArgumentError('missing keyword: :class')}
>   if rest.size > 1
>     unknown = rest.keys - [:class]
>     raise ArgumentError("unknown keyword(s): :#{unknown.join(', :')})
>   end
>
>   arg.is_a?(class_)
> end
> ```
>
> This is very verbose, much less convenient, much less readable, prevents `steep` from generating the proper signature, etc.
>
> We should do the same for pattern match.
>
>
>
> --
> https://bugs.ruby-lang.org/
>
> Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>



-- 
Austin Ziegler • halostatue@gmail.com • austin@halostatue.ca
http://www.halostatue.ca/http://twitter.com/halostatue

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [ruby-core:103360] [Ruby master Feature#17785] Allow named parameters to be keywords
  2021-04-08 15:08 [ruby-core:103305] [Ruby master Feature#17785] Allow named parameters to be keywords marcandre-ruby-core
                   ` (4 preceding siblings ...)
  2021-04-09 17:59 ` [ruby-core:103350] " eregontp
@ 2021-04-10  0:06 ` duerst
  2021-04-10  3:20 ` [ruby-core:103362] " nobu
                   ` (12 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: duerst @ 2021-04-10  0:06 UTC (permalink / raw)
  To: ruby-core

Issue #17785 has been updated by duerst (Martin Dürst).


I think it's not a good idea to introduce special syntax such as `class_` just for the case where arguments are named with keywords. First, the number of keywords is very low, which means that the cases where using a keyword as an argument name makes sense is also very low. Second, there are keywords such as `if` and `else` that are of very doubtful use as variable names anyway. Third, using keywords as variable names inherently increases the cognitive load on the reader and is a source for confusion.

Also, the special meaning of the trailing underscore will be difficult to recognize and understand for most people because it will appear so rarely. And the `_` doesn't match well with the `:` in the argument list. And `_` also is already allowed, so at least in theory, there's a chance of compatibility problems.

And then there's good old `klass`, which did the job for decades. And for those who don't like `klass`, there's `class_`. What's the problem of using `class_` in the argument list if your plan is to use it in the body of the method anyway?

marcandre (Marc-Andre Lafortune) wrote in #note-3:
> Clearly, `class_` is much simpler and much faster than `binding.local_variable_get(:class)`...

What about finding something in between the two? E.g. even just introducing `variable_get` as an alias to `binding.local_variable_get` would make this easier to use. And if this really needs optimization, it could be done, too, but using a different argument name would solve the problem.

With respect to `\`, it reminds me of older languages (such as m4, C, and TeX) where there's a purely string-based level below (or before) the usual structured syntax. Do we want Ruby to descend to that level? Escaping exists inside strings because you don't want the range of data you can handle with a programming language to be restricted by the syntax of the language itself. Also, escaping inside strings is frequent enough for everybody, and occurs in a very similar form across a wide range of programming languages, so that every programmer knows it. Backslashes in front of keywords would be a whole different matter.

There are programming languages where there are no reserved keywords. The one I know and have used is PL/1. If not having any keywords would have been a design goal of Ruby, I'm sure Matz would have found a way to get there. But it wasn't, and I guess it isn't. And as far as I understand, this proposal doesn't get us there.

In conclusion, I think this issue chases a phantom. The trade-off (rarely used obscure syntax to solve a rarely occurring pseudo-problem) is not good. It would introduce some very rarely used edge-case syntax, and wouldn't really make the language any better.

If there are no more urgent kinds of improvements to Ruby syntax that this one, then we know Ruby is in a pretty good place!

----------------------------------------
Feature #17785: Allow named parameters to be keywords
https://bugs.ruby-lang.org/issues/17785#change-91451

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
We should allow named parameters to be keywords and use add a trailing `_` to the corresponding variable:

```ruby
def check(arg, class:)
  arg.is_a?(class_)
end

check(42, class: Integer) # => true
```

Currently, if we want such an API we have to use `**rest`:

```ruby
def check(arg, **rest)
  class_ = rest.fetch(:class) { raise ArgumentError('missing keyword: :class')}
  if rest.size > 1
    unknown = rest.keys - [:class]
    raise ArgumentError("unknown keyword(s): :#{unknown.join(', :')})
  end

  arg.is_a?(class_)
end
```

This is very verbose, much less convenient, much less readable, prevents `steep` from generating the proper signature, etc.

We should do the same for pattern match.



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [ruby-core:103362] [Ruby master Feature#17785] Allow named parameters to be keywords
  2021-04-08 15:08 [ruby-core:103305] [Ruby master Feature#17785] Allow named parameters to be keywords marcandre-ruby-core
                   ` (5 preceding siblings ...)
  2021-04-10  0:06 ` [ruby-core:103360] " duerst
@ 2021-04-10  3:20 ` nobu
  2021-04-10  7:47 ` [ruby-core:103368] " jean.boussier
                   ` (11 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: nobu @ 2021-04-10  3:20 UTC (permalink / raw)
  To: ruby-core

Issue #17785 has been updated by nobu (Nobuyoshi Nakada).


duerst (Martin Dürst) wrote in #note-7:
> What about finding something in between the two? E.g. even just introducing `variable_get` as an alias to `binding.local_variable_get` would make this easier to use. And if this really needs optimization, it could be done, too, but using a different argument name would solve the problem.

In built-in methods written in Ruby, we chose `__builtin.arg!(:in)` form (see timve.rb).

> With respect to `\`, it reminds me of older languages (such as m4, C, and TeX) where there's a purely string-based level below (or before) the usual structured syntax. Do we want Ruby to descend to that level? Escaping exists inside strings because you don't want the range of data you can handle with a programming language to be restricted by the syntax of the language itself. Also, escaping inside strings is frequent enough for everybody, and occurs in a very similar form across a wide range of programming languages, so that every programmer knows it. Backslashes in front of keywords would be a whole different matter.

Agree, and backslashes will be troublesome in `eval` obviously.

----------------------------------------
Feature #17785: Allow named parameters to be keywords
https://bugs.ruby-lang.org/issues/17785#change-91453

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
We should allow named parameters to be keywords and use add a trailing `_` to the corresponding variable:

```ruby
def check(arg, class:)
  arg.is_a?(class_)
end

check(42, class: Integer) # => true
```

Currently, if we want such an API we have to use `**rest`:

```ruby
def check(arg, **rest)
  class_ = rest.fetch(:class) { raise ArgumentError('missing keyword: :class')}
  if rest.size > 1
    unknown = rest.keys - [:class]
    raise ArgumentError("unknown keyword(s): :#{unknown.join(', :')})
  end

  arg.is_a?(class_)
end
```

This is very verbose, much less convenient, much less readable, prevents `steep` from generating the proper signature, etc.

We should do the same for pattern match.



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [ruby-core:103368] [Ruby master Feature#17785] Allow named parameters to be keywords
  2021-04-08 15:08 [ruby-core:103305] [Ruby master Feature#17785] Allow named parameters to be keywords marcandre-ruby-core
                   ` (6 preceding siblings ...)
  2021-04-10  3:20 ` [ruby-core:103362] " nobu
@ 2021-04-10  7:47 ` jean.boussier
  2021-04-10 17:04 ` [ruby-core:103377] " marcandre-ruby-core
                   ` (10 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: jean.boussier @ 2021-04-10  7:47 UTC (permalink / raw)
  To: ruby-core

Issue #17785 has been updated by byroot (Jean Boussier).


> the number of keywords is very low, which means that the cases where using a keyword as an argument name makes sense is also very low.

Variable and keyword names are not purely random though, so I don't think this statistical reasoning makes that much sense. Especially since keywords reserved names that are short and popular: `end`, `class`, etc. If you define method that generate some HTML, a `class:` named parameters is common, if you define a method that deal with period of times, `end:` is common, `if:` is common for methods taking callbacks, etc.

I'm not for adding extra syntax, but I agree with @marcandree that `binding.local_variable_get(:class)` is too slow to be used in many cases.

It would be great if the parser or VM would just optimize it away, but I understand that it's currently very tricky because both `#binding` and `Binding#local_variable_get` could have been redefined. 

----------------------------------------
Feature #17785: Allow named parameters to be keywords
https://bugs.ruby-lang.org/issues/17785#change-91461

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
We should allow named parameters to be keywords and use add a trailing `_` to the corresponding variable:

```ruby
def check(arg, class:)
  arg.is_a?(class_)
end

check(42, class: Integer) # => true
```

Currently, if we want such an API we have to use `**rest`:

```ruby
def check(arg, **rest)
  class_ = rest.fetch(:class) { raise ArgumentError('missing keyword: :class')}
  if rest.size > 1
    unknown = rest.keys - [:class]
    raise ArgumentError("unknown keyword(s): :#{unknown.join(', :')})
  end

  arg.is_a?(class_)
end
```

This is very verbose, much less convenient, much less readable, prevents `steep` from generating the proper signature, etc.

We should do the same for pattern match.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [ruby-core:103377] [Ruby master Feature#17785] Allow named parameters to be keywords
  2021-04-08 15:08 [ruby-core:103305] [Ruby master Feature#17785] Allow named parameters to be keywords marcandre-ruby-core
                   ` (7 preceding siblings ...)
  2021-04-10  7:47 ` [ruby-core:103368] " jean.boussier
@ 2021-04-10 17:04 ` marcandre-ruby-core
  2021-04-19 19:17 ` [ruby-core:103516] " matheusrichardt
                   ` (9 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: marcandre-ruby-core @ 2021-04-10 17:04 UTC (permalink / raw)
  To: ruby-core

Issue #17785 has been updated by marcandre (Marc-Andre Lafortune).


My main objection to `local_variable_get` is that it's super verbose / ugly.

> How would you handle `foo(class_, class:)?`

Setting local `class_` would not happen here. It would only happen if not shadowing an existing variable (except maybe if that other variable was also created the same way)


I like `\class` too.

----------------------------------------
Feature #17785: Allow named parameters to be keywords
https://bugs.ruby-lang.org/issues/17785#change-91470

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
We should allow named parameters to be keywords and use add a trailing `_` to the corresponding variable:

```ruby
def check(arg, class:)
  arg.is_a?(class_)
end

check(42, class: Integer) # => true
```

Currently, if we want such an API we have to use `**rest`:

```ruby
def check(arg, **rest)
  class_ = rest.fetch(:class) { raise ArgumentError('missing keyword: :class')}
  if rest.size > 1
    unknown = rest.keys - [:class]
    raise ArgumentError("unknown keyword(s): :#{unknown.join(', :')})
  end

  arg.is_a?(class_)
end
```

This is very verbose, much less convenient, much less readable, prevents `steep` from generating the proper signature, etc.

We should do the same for pattern match.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [ruby-core:103516] [Ruby master Feature#17785] Allow named parameters to be keywords
  2021-04-08 15:08 [ruby-core:103305] [Ruby master Feature#17785] Allow named parameters to be keywords marcandre-ruby-core
                   ` (8 preceding siblings ...)
  2021-04-10 17:04 ` [ruby-core:103377] " marcandre-ruby-core
@ 2021-04-19 19:17 ` matheusrichardt
  2021-06-09  0:08 ` [ruby-core:104209] " harrison.bachrach
                   ` (8 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: matheusrichardt @ 2021-04-19 19:17 UTC (permalink / raw)
  To: ruby-core

Issue #17785 has been updated by matheusrich (Matheus Melo).


Since we have `__method__`, maybe adding something like `__params__`?

``` ruby
def check(arg, class:)
  arg.is_a?(__params__[:class])
end

check(42, class: Integer) # => true
```


----------------------------------------
Feature #17785: Allow named parameters to be keywords
https://bugs.ruby-lang.org/issues/17785#change-91613

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
We should allow named parameters to be keywords and use add a trailing `_` to the corresponding variable:

```ruby
def check(arg, class:)
  arg.is_a?(class_)
end

check(42, class: Integer) # => true
```

Currently, if we want such an API we have to use `**rest`:

```ruby
def check(arg, **rest)
  class_ = rest.fetch(:class) { raise ArgumentError('missing keyword: :class')}
  if rest.size > 1
    unknown = rest.keys - [:class]
    raise ArgumentError("unknown keyword(s): :#{unknown.join(', :')})
  end

  arg.is_a?(class_)
end
```

This is very verbose, much less convenient, much less readable, prevents `steep` from generating the proper signature, etc.

We should do the same for pattern match.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [ruby-core:104209] [Ruby master Feature#17785] Allow named parameters to be keywords
  2021-04-08 15:08 [ruby-core:103305] [Ruby master Feature#17785] Allow named parameters to be keywords marcandre-ruby-core
                   ` (9 preceding siblings ...)
  2021-04-19 19:17 ` [ruby-core:103516] " matheusrichardt
@ 2021-06-09  0:08 ` harrison.bachrach
  2021-12-02 20:07 ` [ruby-core:106414] " ioquatix (Samuel Williams)
                   ` (7 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: harrison.bachrach @ 2021-06-09  0:08 UTC (permalink / raw)
  To: ruby-core

Issue #17785 has been updated by harrisonb (Harrison Bachrach).


This feels related to [this proposal](https://bugs.ruby-lang.org/issues/16460) I submitted ~1.5 years ago concerning external/internal names for keyword parameters as it would solve this problem somewhat more elegantly:

```ruby
# Only one possible syntax--see above proposal for alternatives
def check(arg, class class_:)
  arg.is_a?(class_)
end

check(42, class: Integer) # => true
```

----------------------------------------
Feature #17785: Allow named parameters to be keywords
https://bugs.ruby-lang.org/issues/17785#change-92388

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
We should allow named parameters to be keywords and use add a trailing `_` to the corresponding variable:

```ruby
def check(arg, class:)
  arg.is_a?(class_)
end

check(42, class: Integer) # => true
```

Currently, if we want such an API we have to use `**rest`:

```ruby
def check(arg, **rest)
  class_ = rest.fetch(:class) { raise ArgumentError('missing keyword: :class')}
  if rest.size > 1
    unknown = rest.keys - [:class]
    raise ArgumentError("unknown keyword(s): :#{unknown.join(', :')})
  end

  arg.is_a?(class_)
end
```

This is very verbose, much less convenient, much less readable, prevents `steep` from generating the proper signature, etc.

We should do the same for pattern match.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [ruby-core:106414] [Ruby master Feature#17785] Allow named parameters to be keywords
  2021-04-08 15:08 [ruby-core:103305] [Ruby master Feature#17785] Allow named parameters to be keywords marcandre-ruby-core
                   ` (10 preceding siblings ...)
  2021-06-09  0:08 ` [ruby-core:104209] " harrison.bachrach
@ 2021-12-02 20:07 ` ioquatix (Samuel Williams)
  2021-12-13  3:25 ` [ruby-core:106630] " Dan0042 (Daniel DeLorme)
                   ` (6 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: ioquatix (Samuel Williams) @ 2021-12-02 20:07 UTC (permalink / raw)
  To: ruby-core

Issue #17785 has been updated by ioquatix (Samuel Williams).


I personally like `\class` too. I run into the issue from time to time.

----------------------------------------
Feature #17785: Allow named parameters to be keywords
https://bugs.ruby-lang.org/issues/17785#change-95054

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
We should allow named parameters to be keywords and use add a trailing `_` to the corresponding variable:

```ruby
def check(arg, class:)
  arg.is_a?(class_)
end

check(42, class: Integer) # => true
```

Currently, if we want such an API we have to use `**rest`:

```ruby
def check(arg, **rest)
  class_ = rest.fetch(:class) { raise ArgumentError('missing keyword: :class')}
  if rest.size > 1
    unknown = rest.keys - [:class]
    raise ArgumentError("unknown keyword(s): :#{unknown.join(', :')})
  end

  arg.is_a?(class_)
end
```

This is very verbose, much less convenient, much less readable, prevents `steep` from generating the proper signature, etc.

We should do the same for pattern match.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [ruby-core:106630] [Ruby master Feature#17785] Allow named parameters to be keywords
  2021-04-08 15:08 [ruby-core:103305] [Ruby master Feature#17785] Allow named parameters to be keywords marcandre-ruby-core
                   ` (11 preceding siblings ...)
  2021-12-02 20:07 ` [ruby-core:106414] " ioquatix (Samuel Williams)
@ 2021-12-13  3:25 ` Dan0042 (Daniel DeLorme)
  2021-12-13 21:31 ` [ruby-core:106645] " Eregon (Benoit Daloze)
                   ` (5 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: Dan0042 (Daniel DeLorme) @ 2021-12-13  3:25 UTC (permalink / raw)
  To: ruby-core

Issue #17785 has been updated by Dan0042 (Daniel DeLorme).


matheusrich (Matheus Richard) wrote in #note-11:
> Since we have `__method__`, maybe adding something like `__params__`?

I _really_ like this idea. Although I would prefer having different names for positional vs keywords, so maybe `__args__` and `__kwargs__`
This was previously suggested in #15049 (for the purpose of forwarding all keyword arguments) but it's very well suited to handling special keywords like `if`/`for`/`class`, without introducing extra syntax or special cases.

----------------------------------------
Feature #17785: Allow named parameters to be keywords
https://bugs.ruby-lang.org/issues/17785#change-95302

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
We should allow named parameters to be keywords and use add a trailing `_` to the corresponding variable:

```ruby
def check(arg, class:)
  arg.is_a?(class_)
end

check(42, class: Integer) # => true
```

Currently, if we want such an API we have to use `**rest`:

```ruby
def check(arg, **rest)
  class_ = rest.fetch(:class) { raise ArgumentError('missing keyword: :class')}
  if rest.size > 1
    unknown = rest.keys - [:class]
    raise ArgumentError("unknown keyword(s): :#{unknown.join(', :')})
  end

  arg.is_a?(class_)
end
```

This is very verbose, much less convenient, much less readable, prevents `steep` from generating the proper signature, etc.

We should do the same for pattern match.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [ruby-core:106645] [Ruby master Feature#17785] Allow named parameters to be keywords
  2021-04-08 15:08 [ruby-core:103305] [Ruby master Feature#17785] Allow named parameters to be keywords marcandre-ruby-core
                   ` (12 preceding siblings ...)
  2021-12-13  3:25 ` [ruby-core:106630] " Dan0042 (Daniel DeLorme)
@ 2021-12-13 21:31 ` Eregon (Benoit Daloze)
  2021-12-14 15:32 ` [ruby-core:106657] " matheusrich (Matheus Richard)
                   ` (4 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: Eregon (Benoit Daloze) @ 2021-12-13 21:31 UTC (permalink / raw)
  To: ruby-core

Issue #17785 has been updated by Eregon (Benoit Daloze).


matheusrich (Matheus Richard) wrote in #note-11:
> Since we have `__method__`, maybe adding something like `__params__`?

I dislike this approach because it will introduce lots of complexity for Ruby implementations, and will likely make the language slower because arguments need to be retained longer than without it.
In the worst case it could even introduce non-obvious memory leaks (because one cannot know if `__params__` would be used, potentially in an `eval` or aliases or so).

JavaScript's `arguments` is a well known PITA for implementations and often seen as hurting optimizations.

----------------------------------------
Feature #17785: Allow named parameters to be keywords
https://bugs.ruby-lang.org/issues/17785#change-95316

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
We should allow named parameters to be keywords and use add a trailing `_` to the corresponding variable:

```ruby
def check(arg, class:)
  arg.is_a?(class_)
end

check(42, class: Integer) # => true
```

Currently, if we want such an API we have to use `**rest`:

```ruby
def check(arg, **rest)
  class_ = rest.fetch(:class) { raise ArgumentError('missing keyword: :class')}
  if rest.size > 1
    unknown = rest.keys - [:class]
    raise ArgumentError("unknown keyword(s): :#{unknown.join(', :')})
  end

  arg.is_a?(class_)
end
```

This is very verbose, much less convenient, much less readable, prevents `steep` from generating the proper signature, etc.

We should do the same for pattern match.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [ruby-core:106657] [Ruby master Feature#17785] Allow named parameters to be keywords
  2021-04-08 15:08 [ruby-core:103305] [Ruby master Feature#17785] Allow named parameters to be keywords marcandre-ruby-core
                   ` (13 preceding siblings ...)
  2021-12-13 21:31 ` [ruby-core:106645] " Eregon (Benoit Daloze)
@ 2021-12-14 15:32 ` matheusrich (Matheus Richard)
  2021-12-14 15:47 ` [ruby-core:106660] " Dan0042 (Daniel DeLorme)
                   ` (3 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: matheusrich (Matheus Richard) @ 2021-12-14 15:32 UTC (permalink / raw)
  To: ruby-core

Issue #17785 has been updated by matheusrich (Matheus Richard).


Eregon (Benoit Daloze) wrote in #note-17:
> matheusrich (Matheus Richard) wrote in #note-11:
> > Since we have `__method__`, maybe adding something like `__params__`?
> 
> I dislike this approach because it will introduce lots of complexity for Ruby implementations, and will likely make the language slower because arguments need to be retained longer than without it.
> In the worst case it could even introduce non-obvious memory leaks (because one cannot know if `__params__` would be used, potentially in an `eval` or aliases or so).
> 
> JavaScript's `arguments` is a well known PITA for implementations and often seen as hurting optimizations.

Yeah, that's really not ideal. Do you think a special syntax (like `\class`, for example) would be less complex for Ruby implementations?

----------------------------------------
Feature #17785: Allow named parameters to be keywords
https://bugs.ruby-lang.org/issues/17785#change-95329

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
We should allow named parameters to be keywords and use add a trailing `_` to the corresponding variable:

```ruby
def check(arg, class:)
  arg.is_a?(class_)
end

check(42, class: Integer) # => true
```

Currently, if we want such an API we have to use `**rest`:

```ruby
def check(arg, **rest)
  class_ = rest.fetch(:class) { raise ArgumentError('missing keyword: :class')}
  if rest.size > 1
    unknown = rest.keys - [:class]
    raise ArgumentError("unknown keyword(s): :#{unknown.join(', :')})
  end

  arg.is_a?(class_)
end
```

This is very verbose, much less convenient, much less readable, prevents `steep` from generating the proper signature, etc.

We should do the same for pattern match.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [ruby-core:106660] [Ruby master Feature#17785] Allow named parameters to be keywords
  2021-04-08 15:08 [ruby-core:103305] [Ruby master Feature#17785] Allow named parameters to be keywords marcandre-ruby-core
                   ` (14 preceding siblings ...)
  2021-12-14 15:32 ` [ruby-core:106657] " matheusrich (Matheus Richard)
@ 2021-12-14 15:47 ` Dan0042 (Daniel DeLorme)
  2021-12-14 16:44 ` [ruby-core:106668] " Eregon (Benoit Daloze)
                   ` (2 subsequent siblings)
  18 siblings, 0 replies; 21+ messages in thread
From: Dan0042 (Daniel DeLorme) @ 2021-12-14 15:47 UTC (permalink / raw)
  To: ruby-core

Issue #17785 has been updated by Dan0042 (Daniel DeLorme).


Eregon (Benoit Daloze) wrote in #note-17:
> because one cannot know if `__params__` would be used, potentially in an `eval` or aliases or so

I agree, with that kind of complexity it wouldn't make sense. But I wasn't thinking of anything so complicated. If the token is lexically present in the method body, assign it a hash of the keyword arguments, just like a local variable. The allocation/cost is only for methods that use it. `eval("__params__")` is simply not supported. To me that's perfectly fine. Sort of like how `eval("v=42"); v` results in undefined `v` error. In all honesty I can't understand why anyone would want to support such an edge case of eval when it results in so much complexity that is simply not needed for the normal case.


----------------------------------------
Feature #17785: Allow named parameters to be keywords
https://bugs.ruby-lang.org/issues/17785#change-95332

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
We should allow named parameters to be keywords and use add a trailing `_` to the corresponding variable:

```ruby
def check(arg, class:)
  arg.is_a?(class_)
end

check(42, class: Integer) # => true
```

Currently, if we want such an API we have to use `**rest`:

```ruby
def check(arg, **rest)
  class_ = rest.fetch(:class) { raise ArgumentError('missing keyword: :class')}
  if rest.size > 1
    unknown = rest.keys - [:class]
    raise ArgumentError("unknown keyword(s): :#{unknown.join(', :')})
  end

  arg.is_a?(class_)
end
```

This is very verbose, much less convenient, much less readable, prevents `steep` from generating the proper signature, etc.

We should do the same for pattern match.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [ruby-core:106668] [Ruby master Feature#17785] Allow named parameters to be keywords
  2021-04-08 15:08 [ruby-core:103305] [Ruby master Feature#17785] Allow named parameters to be keywords marcandre-ruby-core
                   ` (15 preceding siblings ...)
  2021-12-14 15:47 ` [ruby-core:106660] " Dan0042 (Daniel DeLorme)
@ 2021-12-14 16:44 ` Eregon (Benoit Daloze)
  2021-12-17 13:54 ` [ruby-core:106719] " Dan0042 (Daniel DeLorme)
  2021-12-17 18:10 ` [ruby-core:106725] " Eregon (Benoit Daloze)
  18 siblings, 0 replies; 21+ messages in thread
From: Eregon (Benoit Daloze) @ 2021-12-14 16:44 UTC (permalink / raw)
  To: ruby-core

Issue #17785 has been updated by Eregon (Benoit Daloze).


matheusrich (Matheus Richard) wrote in #note-18:
> Eregon (Benoit Daloze) wrote in #note-17:
> Yeah, that's really not ideal. Do you think a special syntax (like `\class`, for example) would be less complex for Ruby implementations?

Yes, then it's only complexity in the lexer and everything else would work unchanged.
The Oz language for instance has `'reserved'` which works like `\class` above.
It's also much simpler conceptually, just help the lexer understand what you want, then everything keeps working as before.
The parser doesn't even need to know about it, it'll just see a identifier token instead of a keyword token.

Dan0042 (Daniel DeLorme) wrote in #note-19:
> In all honesty I can't understand why anyone would want to support such an edge case of eval when it results in so much complexity that is simply not needed for the normal case.

Consistency and referential transparency (anything that works outside eval should work inside eval, true for most things in Ruby).

----------------------------------------
Feature #17785: Allow named parameters to be keywords
https://bugs.ruby-lang.org/issues/17785#change-95341

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
We should allow named parameters to be keywords and use add a trailing `_` to the corresponding variable:

```ruby
def check(arg, class:)
  arg.is_a?(class_)
end

check(42, class: Integer) # => true
```

Currently, if we want such an API we have to use `**rest`:

```ruby
def check(arg, **rest)
  class_ = rest.fetch(:class) { raise ArgumentError('missing keyword: :class')}
  if rest.size > 1
    unknown = rest.keys - [:class]
    raise ArgumentError("unknown keyword(s): :#{unknown.join(', :')})
  end

  arg.is_a?(class_)
end
```

This is very verbose, much less convenient, much less readable, prevents `steep` from generating the proper signature, etc.

We should do the same for pattern match.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [ruby-core:106719] [Ruby master Feature#17785] Allow named parameters to be keywords
  2021-04-08 15:08 [ruby-core:103305] [Ruby master Feature#17785] Allow named parameters to be keywords marcandre-ruby-core
                   ` (16 preceding siblings ...)
  2021-12-14 16:44 ` [ruby-core:106668] " Eregon (Benoit Daloze)
@ 2021-12-17 13:54 ` Dan0042 (Daniel DeLorme)
  2021-12-17 18:10 ` [ruby-core:106725] " Eregon (Benoit Daloze)
  18 siblings, 0 replies; 21+ messages in thread
From: Dan0042 (Daniel DeLorme) @ 2021-12-17 13:54 UTC (permalink / raw)
  To: ruby-core

Issue #17785 has been updated by Dan0042 (Daniel DeLorme).


Dan0042 (Daniel DeLorme) wrote in #note-19:
> Eregon (Benoit Daloze) wrote in #note-17:
> > because one cannot know if `__params__` would be used, potentially in an `eval` or aliases or so
> 
> I agree, with that kind of complexity it wouldn't make sense.

Wait a sec... actually there's precedent for this. `super` does this. It forwards all arguments up the inheritance chain. And it's possible to do `eval("super")`. `__params__` is  like the first half of `super`; just collect the arguments, without the subsequent method call. So it's definitely possible (and already done), technically.



----------------------------------------
Feature #17785: Allow named parameters to be keywords
https://bugs.ruby-lang.org/issues/17785#change-95403

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
We should allow named parameters to be keywords and use add a trailing `_` to the corresponding variable:

```ruby
def check(arg, class:)
  arg.is_a?(class_)
end

check(42, class: Integer) # => true
```

Currently, if we want such an API we have to use `**rest`:

```ruby
def check(arg, **rest)
  class_ = rest.fetch(:class) { raise ArgumentError('missing keyword: :class')}
  if rest.size > 1
    unknown = rest.keys - [:class]
    raise ArgumentError("unknown keyword(s): :#{unknown.join(', :')})
  end

  arg.is_a?(class_)
end
```

This is very verbose, much less convenient, much less readable, prevents `steep` from generating the proper signature, etc.

We should do the same for pattern match.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 21+ messages in thread

* [ruby-core:106725] [Ruby master Feature#17785] Allow named parameters to be keywords
  2021-04-08 15:08 [ruby-core:103305] [Ruby master Feature#17785] Allow named parameters to be keywords marcandre-ruby-core
                   ` (17 preceding siblings ...)
  2021-12-17 13:54 ` [ruby-core:106719] " Dan0042 (Daniel DeLorme)
@ 2021-12-17 18:10 ` Eregon (Benoit Daloze)
  18 siblings, 0 replies; 21+ messages in thread
From: Eregon (Benoit Daloze) @ 2021-12-17 18:10 UTC (permalink / raw)
  To: ruby-core

Issue #17785 has been updated by Eregon (Benoit Daloze).


Dan0042 (Daniel DeLorme) wrote in #note-21:
> Wait a sec... actually there's precedent for this. `super` does this. It forwards all arguments up the inheritance chain. And it's possible to do `eval("super")`. `__params__` is  like the first half of `super`; just collect the arguments, without the subsequent method call. So it's definitely possible (and already done), technically.

`super` (or zsuper in parser terms) does rereads all arguments.
It's already a nightmare in terms of complexity FWIW.

But `__params__(:name)` is far worse, at least zsuper only builds an array not some magic mapping from variable name to value.

Ah and `eval("super")` should probably be deprecated, it seems such a bad idea and I doubt it works well on many Ruby impls.


----------------------------------------
Feature #17785: Allow named parameters to be keywords
https://bugs.ruby-lang.org/issues/17785#change-95410

* Author: marcandre (Marc-Andre Lafortune)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
We should allow named parameters to be keywords and use add a trailing `_` to the corresponding variable:

```ruby
def check(arg, class:)
  arg.is_a?(class_)
end

check(42, class: Integer) # => true
```

Currently, if we want such an API we have to use `**rest`:

```ruby
def check(arg, **rest)
  class_ = rest.fetch(:class) { raise ArgumentError('missing keyword: :class')}
  if rest.size > 1
    unknown = rest.keys - [:class]
    raise ArgumentError("unknown keyword(s): :#{unknown.join(', :')})
  end

  arg.is_a?(class_)
end
```

This is very verbose, much less convenient, much less readable, prevents `steep` from generating the proper signature, etc.

We should do the same for pattern match.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2021-12-17 18:10 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-08 15:08 [ruby-core:103305] [Ruby master Feature#17785] Allow named parameters to be keywords marcandre-ruby-core
2021-04-08 15:21 ` [ruby-core:103307] " mame
2021-04-08 15:23 ` [ruby-core:103308] " zverok.offline
2021-04-08 16:55 ` [ruby-core:103312] " marcandre-ruby-core
2021-04-09 13:04 ` [ruby-core:103346] " jean.boussier
2021-04-09 17:59 ` [ruby-core:103350] " eregontp
2021-04-09 20:57   ` [ruby-core:103354] " Austin Ziegler
2021-04-10  0:06 ` [ruby-core:103360] " duerst
2021-04-10  3:20 ` [ruby-core:103362] " nobu
2021-04-10  7:47 ` [ruby-core:103368] " jean.boussier
2021-04-10 17:04 ` [ruby-core:103377] " marcandre-ruby-core
2021-04-19 19:17 ` [ruby-core:103516] " matheusrichardt
2021-06-09  0:08 ` [ruby-core:104209] " harrison.bachrach
2021-12-02 20:07 ` [ruby-core:106414] " ioquatix (Samuel Williams)
2021-12-13  3:25 ` [ruby-core:106630] " Dan0042 (Daniel DeLorme)
2021-12-13 21:31 ` [ruby-core:106645] " Eregon (Benoit Daloze)
2021-12-14 15:32 ` [ruby-core:106657] " matheusrich (Matheus Richard)
2021-12-14 15:47 ` [ruby-core:106660] " Dan0042 (Daniel DeLorme)
2021-12-14 16:44 ` [ruby-core:106668] " Eregon (Benoit Daloze)
2021-12-17 13:54 ` [ruby-core:106719] " Dan0042 (Daniel DeLorme)
2021-12-17 18:10 ` [ruby-core:106725] " Eregon (Benoit Daloze)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).