Skip to content

Commit 7fdf3e5

Browse files
author
Daniel Němec
committed
Syntax fixes, added Never type and unit type
1 parent bc11bda commit 7fdf3e5

3 files changed

Lines changed: 98 additions & 84 deletions

File tree

tutorials/09_elm-intro.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,31 @@ profile =
618618

619619
_Note_: There are two more complex techniques, how to design data structure - opaque types (next lesson) and phantom types.
620620

621+
### Never type - Never
622+
623+
Never type is a type that doesn't have any values. It's a type that can be specified in a type annotation, but you can't construct a Never value because it is valueless.
624+
625+
### Unit type - ()
626+
627+
Unit type is commonly used as a placeholder for an empty value.
628+
629+
```elm
630+
type alias Message a =
631+
{ code : String
632+
, body : a
633+
}
634+
635+
readMessage : Message String -> String
636+
readMessage message =
637+
...
638+
639+
readEmptyMessage : Message () -> String
640+
readEmptyMessage message =
641+
...
642+
```
643+
644+
_Note_: `readEmptyMessage` function takes Message with an empty body. This is not the same as any value, just an empty one.
645+
621646
### Pattern Matching
622647

623648
```elm
@@ -808,7 +833,7 @@ Elm has several operators for chaining functions and function calls together.
808833
`|>` operator takes a value and a function and applies the function to the value. It is useful when chaining more steps together to write readable code.
809834

810835
```elm
811-
pipe : : a -> (a -> b) -> b
836+
pipe : a -> (a -> b) -> b
812837
pipe =
813838
(|>)
814839

@@ -824,7 +849,7 @@ greet3 maybeName =
824849
`<|` operator is the opposite. It takes a function and a value and apply the function to the value. It is useful to avoid parentheses, the same as `$` in Haskell.
825850

826851
```elm
827-
pipe : : (a -> b) -> a -> b
852+
pipe : (a -> b) -> a -> b
828853
pipe =
829854
(<|)
830855

tutorials/10_elm-tea.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ We can then define our own type for Todo item and a decoder.
3535
```elm
3636
import Json.Decode as Decode
3737

38-
type alias TodoItem =
38+
type alias Todo =
3939
{ id : Int
4040
, label : String
4141
, completed : Bool
@@ -198,12 +198,12 @@ import Email
198198

199199
emailView : Email.Email -> Html msg
200200
emailView =
201-
Email.toString
202-
>> Html.text
201+
Email.toString
202+
>> Html.text
203203

204204
emailView2 : String -> Html msg
205205
emailView2 =
206-
Html.text
206+
Html.text
207207
```
208208

209209
_Note_: From `Email` module, we expose only `Email` type without variant `EmailInternal`. The only way, how to access email value is in this module, no other module does not have access to `EmailInternal` and can use only access function `toString`.

tutorials/11_elm-building-web-apps.md

Lines changed: 67 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ update msg model =
105105
case msg of
106106
SaveUser user ->
107107
(model, Ports.saveUser <| encdeUser user)
108-
...
109108
```
110109

111110
#### Incoming messages
@@ -184,14 +183,14 @@ We use [Browser.application](https://package.elm-lang.org/packages/elm/browser/l
184183

185184
```elm
186185
application :
187-
{ init : flags -> Url -> Key -> ( model, Cmd msg )
188-
, view : model -> Document msg
189-
, update : msg -> model -> ( model, Cmd msg )
190-
, subscriptions : model -> Sub msg
191-
, onUrlRequest : UrlRequest -> msg
192-
, onUrlChange : Url -> msg
193-
}
194-
-> Program flags model msg
186+
{ init : flags -> Url -> Key -> ( model, Cmd msg )
187+
, view : model -> Document msg
188+
, update : msg -> model -> ( model, Cmd msg )
189+
, subscriptions : model -> Sub msg
190+
, onUrlRequest : UrlRequest -> msg
191+
, onUrlChange : Url -> msg
192+
}
193+
-> Program flags model msg
195194
```
196195

197196
The `init` function now gets not only flags but also initial `Url` and navigation `Key` that is needed for changing URL using navigation commands.
@@ -209,90 +208,80 @@ import Html exposing (Html)
209208
import Html.Attributes as Attributes
210209
import Url
211210

212-
-- MAIN
213-
214211
main : Program Flags Model Msg
215212
main =
216-
Browser.application
217-
{ init = init
218-
, view = view
219-
, update = update
220-
, subscriptions = subscriptions
221-
, onUrlChange = UrlChanged
222-
, onUrlRequest = LinkClicked
223-
}
224-
225-
-- MODEL
213+
Browser.application
214+
{ init = init
215+
, view = view
216+
, update = update
217+
, subscriptions = subscriptions
218+
, onUrlChange = UrlChanged
219+
, onUrlRequest = LinkClicked
220+
}
226221

227222
type alias Model =
228-
{ key : Navigation.Key
229-
, url : Url.Url
230-
}
223+
{ key : Navigation.Key
224+
, url : Url.Url
225+
}
231226

232227
type alias Flags =
233-
()
228+
()
234229

235230
init : Flags -> Url.Url -> Navigation.Key -> ( Model, Cmd Msg )
236231
init _ url key =
237-
( { key = key
232+
( { key = key
238233
, url = url
239234
}
240-
, Cmd.none
241-
)
242-
243-
-- UPDATE
235+
, Cmd.none
236+
)
244237

245238
type Msg
246-
= LinkClicked Browser.UrlRequest
247-
| UrlChanged Url.Url
239+
= LinkClicked Browser.UrlRequest
240+
| UrlChanged Url.Url
248241

249242
update : Msg -> Model -> ( Model, Cmd Msg )
250243
update msg model =
251-
case msg of
252-
LinkClicked urlRequest ->
253-
case urlRequest of
254-
Browser.Internal url ->
255-
( model
256-
, Nav.pushUrl model.key <| Url.toString url
257-
)
258-
259-
Browser.External href ->
260-
( model
261-
, Navigation.load href
262-
)
263-
264-
UrlChanged url ->
265-
( { model | url = url }
266-
, Cmd.none
267-
)
268-
269-
-- SUBSCRIPTIONS
244+
case msg of
245+
LinkClicked urlRequest ->
246+
case urlRequest of
247+
Browser.Internal url ->
248+
( model
249+
, Nav.pushUrl model.key <| Url.toString url
250+
)
251+
252+
Browser.External href ->
253+
( model
254+
, Navigation.load href
255+
)
256+
257+
UrlChanged url ->
258+
( { model | url = url }
259+
, Cmd.none
260+
)
270261

271262
subscriptions : Model -> Sub Msg
272263
subscriptions =
273-
always Sub.none
274-
275-
-- VIEW
264+
always Sub.none
276265

277266
view : Model -> Browser.Document Msg
278267
view model =
279-
{ title = "URL Interceptor"
280-
, body =
281-
[ Html.text "The current URL is: "
282-
, Html.b [] [ Html.text <| Url.toString model.url ]
283-
, Html.ul []
284-
[ viewLink "/home"
285-
, viewLink "/profile"
286-
, viewLink "/reviews/the-century-of-the-self"
287-
, viewLink "/reviews/public-opinion"
288-
, viewLink "/reviews/shah-of-shahs"
289-
]
290-
]
291-
}
268+
{ title = "URL Interceptor"
269+
, body =
270+
[ Html.text "The current URL is: "
271+
, Html.b [] [ Html.text <| Url.toString model.url ]
272+
, Html.ul []
273+
[ viewLink "/home"
274+
, viewLink "/profile"
275+
, viewLink "/reviews/the-century-of-the-self"
276+
, viewLink "/reviews/public-opinion"
277+
, viewLink "/reviews/shah-of-shahs"
278+
]
279+
]
280+
}
292281

293282
viewLink : String -> Html msg
294283
viewLink path =
295-
Html.li [] [ Html.a [ Attributes.href path ] [ Html.text path ] ]
284+
Html.li [] [ Html.a [ Attributes.href path ] [ Html.text path ] ]
296285
```
297286

298287
### URL Parsing
@@ -305,19 +294,19 @@ Here's an example form the documentation converting different routes with parame
305294
import Url.Parser exposing ((</>), Parser, int, s, string)
306295

307296
type Route
308-
= Topic String
309-
| Blog Int
310-
| User String
311-
| Comment String Int
297+
= Topic String
298+
| Blog Int
299+
| User String
300+
| Comment String Int
312301

313302
route : Parser (Route -> a) a
314303
route =
315-
oneOf
316-
[ map Topic (s "topic" </> string)
317-
, map Blog (s "blog" </> int)
318-
, map User (s "user" </> string)
319-
, map Comment (s "user" </> string </> s "comment" </> int)
320-
]
304+
oneOf
305+
[ map Topic (s "topic" </> string)
306+
, map Blog (s "blog" </> int)
307+
, map User (s "user" </> string)
308+
, map Comment (s "user" </> string </> s "comment" </> int)
309+
]
321310

322311
-- /topic/wolf ==> Just (Topic "wolf")
323312
-- /topic/ ==> Nothing

0 commit comments

Comments
 (0)