Why does 5 :: Sum Integer
work in the interpreter but given a
newtype NovoTipo a = NovoTipo a
6 :: NovoTipo Integer
does not work?
A literal (or literal data) is data that appears directly in the source code, like the number 5, the character A, and the text “Hello, World.” A value is an immutable, typed storage unit. A value can be assigned data when it is defined, but can never be reassigned. A variable is a mutable, typed storage unit.
Literal values (constants) are exact values (alphabetic or numeric). These values are also the constants that you use in expressions, such as the numeric value 100, or the string "John Smith". You must enclose only string literals within quotes.
A literal is notation for representing a fixed ( const ) value. A variable is storage location associated with a symbolic name (pointed to, if you'd like).
Literals in Python is defined as the raw data assigned to variables or constants while programming. We mainly have five types of literals which includes string literals, numeric literals, boolean literals, literal collections and a special literal None.
You can write numeric literals for any type which has a Num
instance. Sum
has it, while your newtype doesn't.
You can enable GeneralizedNewtypeDeriving
in ghci the following way:
:set -XGeneralizedNewtypeDeriving
Then you can write:
newtype NovoTipo a = NovoTipo a deriving (Num)
And then 6 :: NovoTipo Integer
is well-typed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With