I'm drowning in Int array indexes for arrays of different types. I would love to have a type-safe variant of typealias (at least for these types). Currently I use e.g.,
typealias NodeIndex Int
typealias MatcherIndex Int
typealias ItemIndex Int
...
let matcher: MatcherIndex
let index: NodeIndex
let parent: NodeIndex
var matched = [ItemIndex]()
...
matched[index] = ... // ARGH! no help from compiler
(I'm drawn to parallel-array structures of late.)
I'd like to see a type definition that
- has the same implementation shadow as the raw type
- is treated as a distinct type by the type-checker
- initializes implicitly from raw type?
- explicit raw value cast
- explicit raw value cast for arrays
typedef NodeIndex = Int
let start: NodeIndex = 3
let end: NodeIndex(start.raw + 4)
let offset: Int = end.raw - start.raw
let data = [NodeIndex](..)
let element: Int = data[0].raw
let vector: [Int] = data.raw
As for alternatives, I noticed String.Index
is modeled as an empty frozen struct. Perhaps the runtime cost of an enclosing struct is not bad for smaller data sets, but the array cast is blech.
Corrections welcome: I'm not using the right terminology and might have missed an existing technique or prior discussion. (And if it's not a compelling use-case, I can root around for others.)
Interest? Thoughts?
Thanks!
P.S. - Similar issue: Type Erasure on Built-in And Array Types