2

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?

1

2 Answers 2

1

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 Ints 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'
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.