Mihkel L.
Mihkel L.

Reputation: 1573

Elm package source code

I'd like to see the source code of elm-lang/core that is in use in my project.

My project has:

import Json.Decode exposing (..)

Right now elm compiler says

Cannot find variable `Json.Decode.Decoder`.
`Json.Decode` does not expose `Decoder`. 

From github source I can see that it's exposing Decoder. Would like to see if I have the wrong version of Elm or something.

Just in case - my elm-package.json has

"dependencies": {...
    "elm-lang/core": "5.1.1 <= v < 6.0.0",
     ...
},
"elm-version": "0.18.0 <= v < 0.19.0"

Upvotes: 0

Views: 149

Answers (1)

Chad Gilbert
Chad Gilbert

Reputation: 36375

Your example use in the comments shows that you are using Decoder like this:

on "load" (Decode.Decoder (toString model.gifUrl)

That is indeed a compiler error. While the Json.Decode package exposes a Decoder type, it does not expose a Decoder constructor. This is known as an opaque type, meaning you can't construct a Decoder value yourself, but only by using functions from the Json.Decode package. Opaque types can be exposed by having a module defined like this:

module Foo exposing (MyOpaqueType)

You can specify which constructors are exposed in one of the following ways:

-- only exposes Constructor1 and Constructor2
module Foo exposing (MyType(Constructor1, Constructor2))

-- exposes all constructors of MyType
module Foo exposing (MyType(..))

By your example code, I am inferring that you want some Msg to occur when an image is fully loaded. If that were the case, then you could use something like this:

type Msg
    = ...
    | ImageLoaded String

viewImage model =
    img [ src model.gifUrl, on "load" (Json.Decode.succeed (ImageLoaded model.gifUrl)) ] []

Here is an example of how to handle both the image load and error events.

Upvotes: 2

Related Questions