The Type System represents the different types of values supported by the language. The Type System checks validity of the supplied values, before they are stored or manipulated by the program. This ensures that the code behaves as expected. The Type System further allows for richer code hinting and automated documentation too.
Elm is a statically typed language. Elm has types that are similar to those from other languages.
The number data type represents numeric values. The Elm type system supports the following numeric types −
Sr. No. | Type | Example |
---|---|---|
1 | number − Stores any number | 7 is number type |
2 | Float − Stores fractional values | 7/2 gives 3.5 result as Float |
3 | Int − Stores non-fractional values | 7//2 gives 3 result as Int |
The type number accommodates both fractional and non-fractional values. Open the elm REPL and try the examples given below −
C:\Users\admin>elm repl ---- elm-repl 0.18.0 --------------------------------------------- -------------- :help for help, :exit to exit, more at <https://github.com/elm-lang/elm-repl> ------------------------------------------ -------------------------------------- > 7 7 : number > 7/2 3.5 : Float > 7//2 3 : Int >
The String data type is used to represent a sequence of characters. The Char data type is used to represent a single character. String values are defined within a double quote " and Char values are enclosed within a single quote '.
Sr. No. | Type | Example |
---|---|---|
1 | String − Stores a sequence of characters | "Howcodex" |
2 | Char − Stores fractional values | 'T' |
Open the elm REPL and try the examples given below −
C:\Users\admin>elm repl ---- elm-repl 0.18.0 --------------------------------------- -------------------- :help for help, :exit to exit, more at <https://github.com/elm-lang/elm-repl> -------------------------------------- ------------------------------------------ > "Howcodex" "Howcodex" : String > 'T' 'T' : Char
The Bool data type in Elm supports only two values − True and False. The keyword Bool is used to represent a Boolean value.
Sr. No. | Type | Example |
---|---|---|
1 | Bool − Stores values True or False | 1==1 returns True |
Open the elm REPL and try the examples given below −
C:\Users\dell\elm>elm repl ---- elm-repl 0.18.0 ----------------------------------- ------------------------ :help for help, :exit to exit, more at <https://github.com/elm-lang/elm-repl> ---------------------------------------- ---------------------------------------- > True True : Bool > False False : Bool > 1==1 True : Bool > 1==2 False : Bool > 1 /= 2 -- not equal True : Bool > not True False : Bool > not False True : Bool
Elm supports creating user defined types. For example, consider a payment application. The application needs to store different modes of payment − credit card, debit card and net banking. This can be achieved by defining a custom type and restricting its value to the three acceptable modes of payments.
The following example shows how to make a custom type.
> type PaymentMode = CreditCard|NetBanking|DebitCard > payment1 = CreditCard CreditCard : Repl.PaymentMode > payment2 = DebitCard DebitCard : Repl.PaymentMode > payment3 = UPI -- NAMING ERROR ---------------------------------------------- repl-temp-000.elm Cannot find variable `UPI` 7| payment3 = UPI
In the above example, we created a PaymentMode custom type. Variables payment1 and payment2 are assigned to PaymentMode values. If the value assigned to the variable does not match any of the values defined by the PaymentMode type, the application will throw a syntax error.
Structured data types can be used to store multiple values in a structured format. Elm supports the following structured data types −
These will be discussed in detail in the upcoming chapters.