-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ex02.elm
68 lines (52 loc) · 1.5 KB
/
Ex02.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
-- "Newtypes", "Type Wrappers"
module Ex02 exposing (..)
import Regex
import Regex exposing (regex)
import Result exposing (Result(..))
-- Revisiting something familiar:
type Email = Email String
unwrapEmail : Email -> String
unwrapEmail (Email address) =
address
type EmailError =
InvalidChars
| NoDomain
-- it's a Result now
createEmail : String -> Result EmailError Email
createEmail str =
let
check err rx val =
if (Regex.contains (regex rx) val) then Ok val else Err err
in
(Ok str)
|> Result.andThen (check InvalidChars "^[\\w@+_.-]+$")
|> Result.andThen (check NoDomain "^[^@]+@[^@]+$")
|> Result.andThen (\str -> Ok (Email str))
-- Mixup and Separation
type alias User1 = {
id : String,
name : String,
email : String
}
type UUID = UUID String
type alias User2 = {
id : UUID,
name : String,
email : Email
}
-- User1 has a bunch of Strings, which can be confused.
-- These have context in the form of record fields, though,
-- so it'e not super-likely we're going to mix them up.
-- Unless...
emailFromUser1 : User1 -> String
emailFromUser1 user =
user.email
-- ^== Once we've extracted the email from the record,
-- it's just a String. Which can be passed along
-- accidentally instead of some other String, and
-- the compiler won't care.
emailFromUser2 : User2 -> Email
emailFromUser2 user =
user.email
-- ^== It's still an Email, even after being extracted.
-- It won't get mixed up with any other type (eg. String).