What is the datatypes of namedtuple and hash in crystal language?

my_namedtuple : ??? = {name : “unknown”, job : “coder”}

my_hash : ??? = {“name” => “unknown”, “job” => “code”}

Depends on the data types of the named tuple itself. But basically It’s NamedTuple, then a list of the key names and their types.

my_namedtuple : NamedTuple(name: String, job: String) = {name: "unknown", job: "coder"}

somethingElse : NamedTuple(foo: Int32, bar: String) = {foo: 42, bar: "Crystal <3"}
1 Like

no no tuple and hash what is the type of hash ?

The hash you listed is Hash(String, String). The first is the key type, the second is the value type.

1 Like

named tuple doesn’t work !!!

Post your code please?

1 Like

sorry it works now i just moved : one step ahead that was the issue

1 Like

there is no support for regular tuple ?

There is: Tuple(*T) - Crystal 1.10.1

Note you can always get the type of an expression using typeof:

typeof({name: "unknown", job: "coder"})        # => NamedTuple(name: String, job: String)
typeof({"name" => "unknown", "job" => "code"}) # => Hash(String, String)
2 Likes

Alternatively, you can always start a crystal play playground and see the result.

2 Likes