In the Haskell programming language there are a number of concepts that must be understood by anyone wishing to learn and use the maximum potential of the language. One of these what is is type safety.
My question is what is meant by type safety?
In the Haskell programming language there are a number of concepts that must be understood by anyone wishing to learn and use the maximum potential of the language. One of these what is is type safety.
My question is what is meant by type safety?
Type Safety
is a simple and powerful concept – it means that your app validates types at compile time, and throws an error if you've tried to assign the wrong type to a variable. This way a statically-typed Haskell language prevents type errors (and Plutus does exactly the same way). And Haskell is really good at Type Inference.
Example 1.
Two Int
s can be multiplied:
multiplication :: Int -> Int -> Int
multiplication x y = x * y
main = do
print(multiplication 125 4)
-- Result: 500
Example 2.
Int
and Char
types cannot be multiplied:
multiplication :: Int -> Int -> Int
multiplication x y = x * y
main = do
print(multiplication 125 '4')
-- Result: Couldn't match expected type `Int' with actual type `Char'
This response assumes you know what Types are.
Type safety is pretty much ensuring that you’re putting square pegs into square holes at all times to avoid buggy code.
Example:
A) 3 * 1 -> This is correct because both are integers (Int) and operation can be done.
B) 3 * “foo” —> This is wrong because one is an integer (Int) whereas the other is a string ([Char]), the operation can’t be done.
Expression A is type safe whereas Expression B isn’t.