Comments (2)
Yes, as @devnote-dev pointed out, there is no "shorthand notation". Using a :
denotes a named tuple while =>
denotes a hash. This is somewhat pointed out in the hash docs:
https://crystal-lang.org/reference/1.7/syntax_and_semantics/literals/hash.html
Hashes are typically created with a hash literal denoted by curly braces ({ }) enclosing a list of pairs using => as delimiter between key and value and separated by commas ,.
NamedTuple literal docs don't directly point out that you use :
, but its part of the example.
I'm hesitant to think we need to document these "warnings" explicitly in the literal docs for each type. Having something more descriptive for named tuples like we have for hashes would probably be sufficient. Adding an explicit section in the Crystal for Rubyists
would be a better alternative as this page fits this topic more.
Going to transfer this issue to the crystal-book
repo.
from crystal-book.
The issue is that initializing a hash with symbol keys and constant values it is then instantiated as a NamedTuple instead of a Hash.
[...]
I tried the following code, it works as intended:
thing = {
:foo => "foo",
:bar => "bar"
}
puts typeof(thing) # => Hash(Symbol, String)
thing[:baz] = "baz"
puts thing # => {:foo => "foo", :bar => "bar", :baz => "baz"}
Also note that the syntax in your example is named tuple syntax, not hash syntax which uses =>
.
Here are test cases, however, I have no idea in mind for unpredictable type at compile type to test if the a: b syntax will lead to Hash or NamedTuple in such a case.
Example A and B are hashes because you're using =>
, their types are still known at compile time but it is not as strict as a named tuple, which C and D are because of :
.
Having warnings in the documentation would be useful though, it's not exactly obvious that hashes use a specific syntax which can be confusing if you're coming from Ruby (or JavaScript, Python, etc).
from crystal-book.
Related Issues (20)
- Add more concurrent example/document/blog HOT 1
- The `play` button should use the same version as the documentation
- Dependency Dashboard
- make serve is broken HOT 5
- Lib enum example does not work
- Overview page
- Document `fun` defs
- Document for case statement case nil and others condtions HOT 1
- There is no clear document for how `require "foo/bar/baz"` work. HOT 5
- Documentation on `fun` argument conversion is incorrect HOT 1
- [crystalforrubyists] Add concurrency and TypeProf
- make serve HOT 7
- Symbol enum auto-casting
- Documentation for `include` inside lib structs
- Fix about compile time flag -Dno_restrictions_augmenter HOT 1
- What is the correct target name for macOS? HOT 2
- Document Tracing Feature
- Closing as spam
- Dark Mode for DOCS. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from crystal-book.