Can I make an alias of a NamedTuple?

Crystal doesn’t seem to allow this:
alias MyTuple = NamedTuple(foo : String, bar : Int32)

What’s the idiomatic way of creating my own type that is like a NamedTuple?

Inside the parentheses, you’ve got two things that parse as a argument in a method signature, but what you want is for it to parse as a named argument passed to a method. Syntactically, all it means is you remove the space before the :.

alias MyTuple = NamedTuple(foo: String, bar: Int32)
1 Like

Ah… Thank you!

The Type Grammar also supports alias MyTuple = {foo: String, bar: Int32}. Tho depending on your use case, I’d probably consider using Top Level Namespace - Crystal 1.11.2 instead.

1 Like

To answer the question as stated though, I’d suggest using a record.
record(Typename, field1 : Type, field2 : Type2). It also has a blockform if you need more methods than just the constructor and getters.

EDIT: Hmm, turns out I wasn’t the first to suggest that. Blacksmoke, you should put better labels on your links :)

1 Like

It’s unfortunate that when you link to a specific method or something the link gets transformed to the title of the page :confused:. Guess you just have to create manual hyperlinks to get it to display properly.