Back again, so thanks for all responses to this any previous questions. I’m grateful that there’s such a community out there so supportive of rank beginners.
I have spent the weekend trying and failing fully to grasp Maybe. The start point is this: I have a Model which contains a list of photos, one of which is the current one.
type alias Model =
{ photos : List Photo
, selectedPhoto : Maybe Photo
}
The photo list is initially empty, so the selectedPhoto has to be of type Maybe Photo and is initialised to Nothing. So far so good.
Photo is itself a record with, for the time being, only one attribute, its url; more precisely the last part of its file name (e.g. “1.jpeg”, “2.jpeg” …). I have made it a record because I’d like to add metadata associated with each photo at some stage (location, camera, lens, aperture, shutter speed etc.)
Type alias Photo =
{ url : String }
I can successfully read into model.photos a list of urls and set model.selectedPhoto:
update msg model =
case msg of
GotPhotos result -> – GotPhotos is the Msg passed to and returned by Http.get
case result of
Ok responseString ->
let
pictures : List Photo
pictures = List.map (\str-> { url = str }) (String.split “,” responseString)
selected : Maybe Photo
selected = List.head pictures
in
( { model | photos = pictures, selectedPhoto = selected }, Cmd.none )
Err httpError ->
( { model | status = ErrorOnLoading "Server Error"}, Cmd.none )
Where I am utterly stuck is trying to recover the url from model.selectedPhoto so that I can concatenate it with a constant directory name (urlPrefix) to give the full path as the “scr” in an Element.image call. Everything I have tried just gives compiler errors that, although fulsome, aren’t helping me.
For example:
src = urlPrefix ++ model.selectedPhoto.url
gives the error:
The value at .selectedPhoto is a:
Maybe Photo
But I need a record with a url field!
I know that, but how do I get it!!!
I clearly don’t understand Maybe as well as I need and the tutorials all seem to deal with much simpler cases. Any help would be gratefully received …