But a NamedTuple can’t have numeric keys. So I get around it by parsing the object into a Hash and wrapping the result into a struct to access the properties. This approach works with simple objects like the one above, but it becomes tedious with complex objects involving unions.
So I was wondering, what’s the reason why a NamedTuple can’t have numeric keys? And is there another solution I missed?
My last resort would be forking the library and making @[CBOR::Field(key: 1)] work so I can map the whole object to a Struct. But maybe I’m missing something.
NamedTuple mainly exists to support named argument passing, and named arguments can’t have number literals as their names. You have to turn numbers into strings similarly in that case:
def foo(*, 1 x) # Error: unexpected token: "1"
end
foo(1: "a") # Error: expecting token ')', not ':'
def foo(*, "1" x) # okay
end
foo("1": "a") # okay