What about keyword arguments? #32
-
|
Interesting project! One thing that I am wondering is how are keyword arguments handled? I can't find it in the documents, all of the examples seem to use just positional arguments. And trying to compile regular Ruby with keyword arguments of course treats the keyword values as types, since the syntax is the same. For example, how can this code be expressed as T-Ruby? def greet(name: "default")
"Hello, #{name}!"
end
puts greet(name: "World") |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 17 replies
-
|
It was implemented recently, wasn't it! |
Beta Was this translation helpful? Give feedback.
-
|
I've reopened this topic because it's still worth discussing. I actually quite liked my syntactic solution for keyword arguments. I now agree that the current solution is potentially flawed, and I believe there needs to be more discussion about new ideas for syntactic improvements. |
Beta Was this translation helpful? Give feedback.
-
Ruby → T-Ruby Compatibility Test CasesWhen considering the "All Ruby code is T-Ruby code" principle, we need to ensure that valid Ruby code behaves identically in T-Ruby. Here are the key test cases that should be validated: # test_ruby_compatibility.rb
module Compatibility
String = "constant" # Constant with same name as class
# 1. Keyword default value is a class name (most dangerous case)
def self.create(format: String, type: Integer)
end
# 2. All parameter types combined
def self.process(x, *args, name:, retries: 3, **opts, &block)
end
# 3. Hash literal - value is class name
TYPES = { string: String, integer: Integer }
# 4. Method call - keyword argument with class value
def self.run
create(format: String, type: Integer)
end
# 5. Pattern matching (Ruby 3.0+)
def self.match(obj)
case obj
in { name: String => n }
n
end
end
# 6. Lambda with keyword parameter
TRANSFORM = ->(x, default: nil) { x || default }
# 7. Heredoc containing type-like text
QUERY = <<~SQL
SELECT name: String FROM users
SQL
endTest Matrix
Core Issue:
|
| Context | Ruby Meaning | T-Ruby Misinterpretation Risk |
|---|---|---|
| Method definition | Default value | Type annotation |
| Method call | Argument value | ? |
| Hash literal | Value | ? |
| Pattern matching | Type check | ? |
The key question is: How well can the parser distinguish context?
Any alternative syntax (Options 3-6) should pass all these test cases to maintain Ruby compatibility.
Beta Was this translation helpful? Give feedback.
It seemed slightly different. This is probably it.
↓