Coder Social home page Coder Social logo

Comments (2)

Blacksmoke16 avatar Blacksmoke16 commented on August 16, 2024 1

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.

devnote-dev avatar devnote-dev commented on August 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.