3 ms·
Not quite the same meaning of 'variant'.
by cchooper 18y ago
Not quite the same meaning of 'variant'.
- jganetsk 18y agoOh, I see... we're talking about Visual Basic: "Dim X as Variant". Well then, Paul Graham is right. That is a provincial spelling of "dynamic typing". Incidentally, I don't think variants in the Hindley-Milner sense of the word get nearly enough love from hackers. And, in fact, they do correspond to dynamic typing. data Dyn = I Integer | S String | D Double | F Dyn -> Dyn | ... And now you can use your statically typed language as a dynamically typed one.
- cchooper 18y agoBut the language will still be statically typed, as the correct resolution of a Dyn into an Integer, String etc, will still have to be ensured at compile time.
- jganetsk 18y agoNo, it will be ensured at run time, as all unboxing happens through pattern matches (which, btw, is EXACTLY what happens with a dynamically typed language). For example, I can define... (+) :: Dyn -> Dyn -> Dyn (+) (I x) (I y) = I (x + y) -- integer addition (+) (S x) (S y) = S (x ++ y) -- string concat So I can now use the + operator on variables of type Dyn. If a value has the wrong tag, then a pattern matching exception is thrown. Once I get enough primitive operations defined, I will never really have to manually unbox a value of Dyn type again. The one difficulty is emulating objects, because we would not have good syntax for arbitrary method calls. We could take a Smalltalk-like or Erlang-like approach, where the object takes message... the message being of type Dyn. We should really look at dynamically typed languages as subsets of statically typed languages. In other words, they are not untyped, they are unityped. In other words, statically typed, but with exactly one type. This is the relationship between typed and untyped lambda calculus (untyped lambda calculus is actually typed, where every term has type Mu X. X -> X)
- cchooper 18y agoThis is getting into details of semantics, but I'm not sure if your example is really shows that the language supports dynamic typing. It's more like an example of implementing dynamic typing in a static language. After all, you're not using the language's type system to do run-time type checking. You're using run-time pattern matching as a substitute. Of course, I'm just being pernickety, but that's how I am :)