Alex Craft
Alex Craft

Reputation: 15346

Generic NamedTuple in Crystal

Is there a way to use NamedTuple as Generics? Something like:

alias JsonCommand = NamedTuple(T){
  name : String
  data : T
}

command : JsonCommand(String) = { name: "some command", data: "some data" }

Upvotes: 1

Views: 141

Answers (1)

Johannes Müller
Johannes Müller

Reputation: 5661

No.

The main reason why named tuples exist in the language is for implementing named arguments.

For every other use case it's recommended to use a struct instead and it can be used with generics.

record JSONCommand(T), name : String, data : T

command = JSONCommand.new(name: "some command", data: "some data")

Upvotes: 2

Related Questions