Skip to content
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

Merged
merged 3 commits into from Jan 30, 2017
Merged

Fix example for NamedTuple.from #3911

merged 3 commits into from Jan 30, 2017

Conversation

mjago
Copy link
Contributor

@mjago mjago commented Jan 17, 2017

This is a fix to the NamedTuple.from example which should close #3792

# NamedTuple(foo: String, bar: Int64).from({:foo => "world", :bar => 2}).class # => {foo: String, bar: Int64}
# ```
# num_or_str : Int32 | String
# num_or_str = 42
Copy link
Member

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.

Copy link
Contributor Author

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
Copy link
Member

@bcardiff bcardiff Jan 17, 2017

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
Copy link
Member

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}
Copy link
Contributor

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.

Copy link
Member

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bcardiff you're right, @mjago excuse my brain damage!

@mjago
Copy link
Contributor Author

mjago commented Jan 30, 2017

@bcardiff Are you still waiting for changes on this?

@mverzilli mverzilli merged commit 4c15dec into crystal-lang:master Jan 30, 2017
@mverzilli
Copy link

@mjago I don't think he is. Thanks for this ❤️ .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NamedTuple.from should cast?
4 participants