I'm trying to understand newtype and thought this would work:
module NT where
newtype X = X Double
newtype Y = Y Double
doit :: X -> Y -> Double
doit x y = x + y
x = X 1.1
y = Y 2.2
-- doit x y should work
-- doit y x should error
The first error produced is:
NT.hs:7:12: error:
• Couldn't match expected type ‘Double’ with actual type ‘X’
• In the expression: X x + Y y
In an equation for ‘doit’: doit x y = X x + Y y
|
7 | doit x y = X x + Y y
|
I get that the types don't match, I just don't understand how to get around it. I thought wrapping Double like this could be used to prevent the mixup of x and y in doit.
Is this true, or am I misunderstanding?