Convert Int to Number?

PureScript uses what’s called a “Hindley-Milner” type system, which allows it to do type inference. But type inference and subtyping don’t play well together. So when you have a value in PureScript, it is always of a single type. You couldn’t have the value 5 have both type Int and type Number. Instead what you see is that you have one type, but that type might implement multiple type classes. So for example, the value "hello" has type String, but also an instance of Semigroup and Monoid and Ord and Eq, etc. which are all classes. You could write a function that takes in just a String, or you could write a function that takes in any Ord, and then you could pass a String into that function.

Haskell, the language that PureScript drew most of its inspiration from, has a class called Num, and both Int and Number would belong to the Num class. This is closest to what you’re looking for, but PureScript doesn’t have the Num class directly. See this post where Harry states the closest PureScript equivalent is the Ring class. So for example, I could write a function

f :: Number -> Number -> Number
f x y = (x + y) * (x - y)

and then I can’t call it with an Int without first calling toNumber. Or I could write the function as

f :: forall a. Ring a => a -> a -> a
f x y = (x + y) * (x - y)

and now I can call f 8.0 5.0 or f 8 5 and both work.

3 Likes