Ruby added this in 3.1:
Here’s a small snippet:
x, y = 100, 200
# => [100, 200]
# In hash literal
h = {x:, y:}
# => {:x=>100, :y=>200}
# In method call
p(x:, y:)
# prints: "{:x=>100, :y=>200}"
In Crystal that would translate to named tuples and calls. Given that in Crystal you can use named arguments in every call, I think it could make code more succinct, specially when forwarding named arguments.
Searching for (\w+):\s(\1)\b
in the compiler+std source code, there are 1671 matches, which is a lot!
Some random examples I found:
def self.build(io : IO, version : String? = nil, encoding : String? = nil, indent = nil, quote_char = nil) : Nil
build_fragment(io, indent: indent, quote_char: quote_char) do |xml|
xml.start_document version, encoding
yield xml
# omit end_document because it is called in build_fragment
end
end
becomes:
def self.build(io : IO, version : String? = nil, encoding : String? = nil, indent = nil, quote_char = nil) : Nil
build_fragment(io, indent:, quote_char:) do |xml|
xml.start_document version, encoding
yield xml
# omit end_document because it is called in build_fragment
end
end
Another one:
request = HTTP::Request.from_io(
input,
max_request_line_size: max_request_line_size,
max_headers_size: max_headers_size,
)
becomes:
request = HTTP::Request.from_io(
input,
max_request_line_size:,
max_headers_size:,
)
And this:
def self.from_filetime(filetime) : ::Time
seconds, nanoseconds = filetime_to_seconds_and_nanoseconds(filetime)
::Time.utc(seconds: seconds, nanoseconds: nanoseconds)
end
becomes:
def self.from_filetime(filetime) : ::Time
seconds, nanoseconds = filetime_to_seconds_and_nanoseconds(filetime)
::Time.utc(seconds:, nanoseconds:)
end
What are your thoughts about this? Should we add it?