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


> What is `foo`?

In this case, `foo` would be a (JavaScript) `Object`. In using this syntax, you are presuming that `foo` would have the properties `nameOfOneProperty` or `nameOfAnotherProperty`, though if it did not have either of those specific properties, the corresponding identifiers (`newNameForTheProperty` and `newNameForTheOtherProperty` would be initialized to `undefined`)

> Isn't this syntax already reserved/used for keyword arguments?

Yes, this would be an extension of that syntax, though perhaps I'm not understanding you.

> matz knows crystal; he even donated a seizable sum to crystal way before that
> donation-webpage + "hall of fame" for crystal existed. :-)

Yes sorry, I wasn't trying to imply ignorance--I'm sure many Ruby contributors are familiar.

 
> I think pattern matching allows for destructuring?

This is my mistake. I had not kept up with what made it in to pattern matching. This somewhat reduces the need for the feature suggestion, but I still think this would be an improvement in terms of expressiveness in this case:

```ruby
# In all examples, external API is `move(from: 'foo/bar/biz', to: 'baz/boz/buzz')`

# 2.0+(?) with just normal assignment
def move(from:, to:)
  source = from
  destination = to
  # ...
end

# 2.7+ with pattern matching syntax
def move(from:, to:)
  # Must construct a Hash literal in order to make use of both pattern matching syntax benefits of keyword params
  case { from: from, to: to }
  in { from: source, to: destination }
    # ...
  end
end

# Note that in both of the above examples, `from` and `to` are also valid (duplicate) identifiers

# Proposed syntax
def move(from source:, to destination:)
  # ...
end
```

I feel like there are many examples (and I can come up with more) as this can be useful whenever the following circumstances occur:
1. The method name is a verb
2. The arguments may be disambiguated with keyword params that are prepositions

It's possible there are other cases where these are useful, but even just this above category of cases is large.

> By the way, different languages use different syntaxes/patterns; it's not often
> easy or possible or wanted to translate 1:1.

I definitely agree in general. I pick this piece as it seems fairly agnostic to the distinctions between Crystal & Ruby (type-system, compilation, etc.)

> But I don't want to sound discouraging  either - just be prepared to update/modify the suggestion. What
> helps the most is to focus on clear use cases; the ruby core team often 
> recommends to have a clear use described case.

Happy to modify/expand!
 
> Don't worry that this is your
> first suggestion, everyone has to start at some point in time to contribute
> to (if wanted). :-)

Thank you for the encouragement :)

> Though I don”Ēt remember exactly, once I proposed a similar syntax.
And it was rejected then `local_variable_get` was introduced instead.

Do you know where I might find that conversation? I'm not sure if I understand how `local_variable_get` would help in this case. I did a brief search for `local_variable_get` in the issue tracker but the only results seem to be relevant to for resolving keyword args that collide with resolved keywords in Ruby.


----------------------------------------
Feature #16460: External names for keyword parameters in method definitions
https://bugs.ruby-lang.org/issues/16460#change-83467

* Author: harrisonb (Harrison Bachrach)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
Hello! This is my first time filing an issue and I was unable to find anything similar. I apologize if one already exists.

In other languages (such as JavaScript, Swift, and Crystal), it is possible to have two names for a given keyword argument: one that is used in method invocation, and one used in the method definition. Here is an example from Crystal (which has syntax very similar to Ruby):

```crystal
def increment(value, by amount)
  value + amount
end

increment(value: 5, by: 10)
```

This helps create more readable method invocations and definitions. It would be especially helpful in Ruby as the language lacks a destructuring syntax for hashes/keyword args. This is unlike JavaScript, where you can do something like 
```javascript
const { nameOfOneProperty: newNameForTheProperty, nameOfAnotherProperty: newNameForTheOtherProperty } = foo;
```


I'm thinking that such a change would pair nicely with the new 3.0 keyword argument changes. A suggested syntax might be

```ruby
def name(external_name internal_name:)
  # ...
end




-- 
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>