-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix example for NamedTuple.from #3911
Conversation
# NamedTuple(foo: String, bar: Int64).from({:foo => "world", :bar => 2}).class # => {foo: String, bar: Int64} | ||
# ``` | ||
# num_or_str : Int32 | String | ||
# num_or_str = 42 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not creates a union:
num_or_str : Int32 | String
num_or_str = 42
pp typeof(num_or_str) # => Int32
Either num_or_str = 42 || "a"
, num_or_str = 42.as(Int32 | String)
or num_or_str = rand >= 0.0 ? 42 : "a"
are the usual idioms used for one liners unions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah thanks - thats interesting since:
str_or_num : String | Int32
str_or_num = 1_i64 => type must be (Int32 | String), not Int64
I will use num_or_str = 42.as(Int32 | String)
thanks
# | ||
# num_or_str = "a string" | ||
# NamedTuple(name: String, val: Int32).from({"name" => "number", "val" => num_or_str}) # => can't cast String to Int32 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To keep consistency, I think the text should be:
# raises TypeCastError (cast from String to Int32 failed)
check PR #3820 and https://github.com/maiha/comment-spec.cr
# | ||
# num_or_str = "a string" | ||
# NamedTuple(name: String, val: Int32).from({"name" => "number", "val" => num_or_str}) # => can't cast String to Int32 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To keep consistency, I think the text should be:
# raises TypeCastError (cast from String to Int32 failed)
check PR #3820 and https://github.com/maiha/comment-spec.cr
# NamedTuple(foo: String, bar: Int64).from({:foo => "world", :bar => 2}).class # => {foo: String, bar: Int64} | ||
# ``` | ||
# num_or_str = 42.as(Int32 | String) | ||
# NamedTuple(name: String, val: Int32).from({"name" => "number", "val" => num_or_str}) # => {name: "number", val: 42} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New examples are using Hash
instead of NamedTuple
for an argument. Also, I'd stick to one-liners in this particular case, otherwise you must to reread previous line to get the value of num_or_str
instead of just having it inline along with all expression.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Sija the samples were using Hash before. The change is whether the keys are Strings or Symbols. I think is good to show that both will work.
I think is ok to keep num_or_str
. The sample is too long, yet simple. The name of the the variable is clear enough I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bcardiff Are you still waiting for changes on this? |
@mjago I don't think he is. Thanks for this ❤️ . |
This is a fix to the NamedTuple.from example which should close #3792