3 ms·
I'm not really sure of Rust's semantics, but this sort of thing is achieved using GADTs in Haskell. They look like this data T a where TI :: Int -
by tel 13y ago
I'm not really sure of Rust's semantics, but this sort of thing is achieved using GADTs in Haskell. They look like this
data T a where
TI :: Int -> T Int
TS :: String -> T String
which lets you reflect a value up into its type. Something similar but more implicit seems to be going on with that Rust example.
Another method is kind of a hack: unify the type variable with an unused argument. This looks like
data T a b = T b -- we want to find a way to choose a
setA :: a -> T a b -> T a b
setA _ t = t -- Just returns the second argument
-- BUT tells the compiler the `a` type unifies
-- with the type of the first argument
> :t setA Int (T "foo")
setA Int (T "foo") :: T Int String