12. class User:
def __init__(self, name, twitter):
self.name = name
self.twitter = twitter
user = User('Eduardo Cuducos', 'cuducos')
def get_social_media(user):
return user.facebook
get_social_media(user)
12 / 73
13. class User:
def __init__(self, name, twitter):
self.name = name
self.twitter = twitter
user = User('Eduardo Cuducos', 'cuducos')
def get_social_media(user):
return user.facebook
get_social_media(user)
AttributeError: 'User' object has no attribute 'facebook'
13 / 73
14. type alias User =
{ name : String
, twitter : String
}
getSocialMedia : User -> String
getSocialMedia user =
user.facebook
14 / 73
15. type alias User =
{ name : String
, twitter : String
}
getSocialMedia : User -> String
getSocialMedia user =
user.facebook
-- TYPE MISMATCH ------------------------------------------------------ Main.elm
`user` does not have a field named `facebook`.
15| user.facebook
^^^^^^^^^^^^^
The type of `user` is:
User
Which does not contain a field named `facebook`.
Hint: The record fields do not match up. One has name and twitter. The other has
facebook.
15 / 73
16. type alias User =
{ name : String
, twitter : String
}
getSocialMedia : User -> String
getSocialMedia user =
user.twitter
16 / 73
17. type alias User =
{ name : String
, twitter : String
}
getSocialMedia : User -> String
getSocialMedia user =
user.twitter
Success! Compiled 1 module.
17 / 73
18. Elm não te deixa com
o undefined na mão!
18 / 73
29. users = []
first_user_name(users)
IndexError: list index out of range
def first_user_name(users):
first_user, *_ = users
return first_use.name
ValueError: not enough values to unpack (expected at least 1, got 0)
29 / 73
30. firstUserName : List User -> String
firstUserName users =
let
firstUser = List.head users
in
firstUser.name
30 / 73
31. firstUserName : List User -> String
firstUserName users =
let
firstUser = List.head users
in
firstUser.name
-- TYPE MISMATCH --------------------------------------------- Main.elm
`firstUser` does not have a field named `name`.
29| firstUser.name
^^^^^^^^^^^^^^
The type of `firstUser` is:
Maybe User
Which does not contain a field named `name`.
31 / 73
32. firstUserName : List User -> String
firstUserName users =
case List.head users of
Just user ->
user.name
Nothing ->
"User not found"
32 / 73
33. firstUserName : List User -> String
firstUserName users =
case List.head users of
Just user ->
user.name
Nothing ->
"User not found"
Success! Compiled 1 module.
33 / 73
34. Elm te obri a a preparar a
aplicação para casos raros, extremos!
34 / 73
45. tomorrow(None)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'
tomorrow('NaN')
ValueError: invalid literal for int() with base 10: 'NaN'
45 / 73
47. tomorrow : String -> Int
tomorrow today =
(String.toInt today) + 1
-- TYPE MISMATCH ------------------------------------------------------ Main.elm
The left argument of (+) is causing a type mismatch.
45| (String.toInt today) + 1
^^^^^^^^^^^^^^^^^^
(+) is expecting the left argument to be a:
number
But the left argument is:
Result String Int
47 / 73
48. tomorrow : String -> Int
tomorrow today =
case String.toInt today of
Ok day ->
day + 1
Err _ ->
Debug.crash "TODO"
48 / 73
49. tomorrow : String -> Int
tomorrow today =
case String.toInt today of
Ok day ->
day + 1
Err _ ->
Debug.crash "TODO"
Success! Compiled 1 module.
49 / 73
50. Elm te obri a a se antecipar a erros
em tempo de execução.
50 / 73
63. type Msg = Increment | Decrement
update : Msg -> Int -> Int
update msg model =
case msg of
Increment -> model + 1
Decrement -> model - 1
view : Int -> Html Msg
view model =
p
[]
[ button [ onClick Increment ] [ text "+" ]
, text (toString model)
, button [ onClick Decrement ] [ text "-" ]
]
63 / 73
64. type Msg = Increment | Decrement
update : Msg -> Int -> Int
update msg model =
case msg of
Increment -> model + 1
Decrement -> model - 1
view : Int -> Html Msg
view model =
p
[]
[ button [ onClick Increment ] [ text "+" ]
, text (toString model)
, button [ onClick Decrement ] [ text "-" ]
]
‑ 21 +
64 / 73
65. view : Int -> Html Msg
view model =
p
[]
[ button [ onClick Increment ] [ text "+" ]
, text (toString model)
, button [ onClick Decrement ] [ text "-" ]
, button [ onClick Double ] [ text "2x" ]
]
65 / 73
66. view : Int -> Html Msg
view model =
p
[]
[ button [ onClick Increment ] [ text "+" ]
, text (toString model)
, button [ onClick Decrement ] [ text "-" ]
, button [ onClick Double ] [ text "2x" ]
]
-- NAMING ERROR ------------------------------------------------------- Main.elm
Cannot find variable `Double`
82| , button [ onClick Double ] [ text "2x" ]
66 / 73
68. type Msg = Increment | Decrement | Double
-- MISSING PATTERNS --------------------------------------------------- Main.elm
This `case` does not have branches for all possibilities.
68|> case msg of
69|> Increment ->
70|> model + 1
71|>
72|> Decrement ->
73|> model - 1
You need to account for the following values:
Main.Double
Add a branch to cover this pattern!
68 / 73
69. update : Msg -> Int -> Int
update msg model =
case msg of
Increment -> model + 1
Decrement -> model - 1
Double -> model * 2
69 / 73
70. update : Msg -> Int -> Int
update msg model =
case msg of
Increment -> model + 1
Decrement -> model - 1
Double -> model * 2
Success! Compiled 1 module.
70 / 73
71. A arquitetura do Elm te dá arantia de que todas
as ações dos usuários estão previstas!
71 / 73