4 ms·
How does the compiler know that TI(1) is of type T<int>, and TS("foobar") is T<~string>? Does it infer the type of the enum constructors from their arguments? D
by Anderkent 13y ago
How does the compiler know that TI(1) is of type T<int>, and TS("foobar") is T<~string>? Does it infer the type of the enum constructors from their arguments? Does that mean the arities must be the same?
Say something like:
enum Node<A> {
StringNode(Node, Node, ~string)
IntNode(Node, Node, int)
VoidNode(Node, Node)
}
Is there any way of declaring that StringNode gives you Node<~string>, and IntNode gives Node<int>?
- tel 13y agoI'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
- moomin 13y agoWhat he said. The entire post seems to just quitely ignore the question of how let works in the first place.
- koomi 13y agoSee andolanra's answer below: https://news.ycombinator.com/item?id=6219283 https://news.ycombinator.com/item?id=6219283