4 ms·
Does anyone else prefer the Ruby typed parameter syntax over the Python one even though the author says it would likely be rejected by Rubyists? Here's an examp
by nickjj 3y ago
Does anyone else prefer the Ruby typed parameter syntax over the Python one even though the author says it would likely be rejected by Rubyists? Here's an example from the post:
# Python's current type hints
def readlines(name: str, chomp: bool = False):
vs
# Ruby's potentially valid type hints that don't exist today
def readlines(String name, Boolean chomp: false)
I know with Ruby you often want to put more important things first, because it deserves to be the center of attention but in the above case I think the 2nd one is so much more readable at a glance. There's less syntactic noise with the Ruby example because it avoids the equals sign and excess colons. You could also make a case when calling a function both the type and name of the param itself are really important. If you think of this as a user experience exercise, someone calling your function is the user. As a user I very much like the idea of knowing what I need to pass in and "what I need to pass in" is a combination of a descriptive name and potential type.
At a fundamental level, imperative or partially imperative languages are nice because you can typically build upon what you know to do new things in incremental steps.
For example if you know how to make a variable and a loop you can combine your knowledge to make a nested loop and mutate a variable without learning any new concepts.
The Ruby example feels like you're incrementally adding something new. I don't really need to learn anything new to understand I'm adding types. It's also very clean looking. The Python one looks like a different language. I mean, I know both Python and Ruby and use them regularly but `chomp: bool = False` just looks icky to me. It's like some weird hybrid Python / Ruby combo and takes a few seconds to understand.
- jaxn 3y agoIn Ruby, I would see your example as name having the value str. Chomp would have the value of the result of the operation assigning bool the value of False, which I result of the operation might be true, so would chomp have the value of true? Either way, it looks like an assignment instead of comparison error, so I’d kick it back in a PR no matter how it worked.
- caseyohara 3y agoI think you confused the examples, the first one is Python.
- eric-hu 3y agoThe Ruby example you gave looks good for these simple cases. I feel like this could break down for more complex cases though. What about generic types? Or what Sorbet calls Shapes (e.g. the input type is a hash with 2 specific keys -- stuff like this is typical with JSON ingestion in TS).