In GHCI if I do..
let x = 1 + 2
then
:sprint x
I get
x = _
then if I do
x
3
then..
:sprint x
I still get
x = _
However if I do
let x = 1 + 2 :: Int
Then the second :sprint x gives me
3
Just wondering why that is?
In GHCI if I do..
let x = 1 + 2
then
:sprint x
I get
x = _
then if I do
x
3
then..
:sprint x
I still get
x = _
However if I do
let x = 1 + 2 :: Int
Then the second :sprint x gives me
3
Just wondering why that is?
Look at the type of 1 + 2
: it's Num a => a
. That means it can be any numeric type, specified by whoever uses x
. So if you evaluate it once, GHCI's defaulting rules mean it defaults to Int
, and then you evaluate x :: Int
, which is indeed 3 :: Int
. However, that can't be saved as the value for x
, because someone might later try to use it as x :: Double
, for example, and the answer would be different.