ccapndave/elm-reflect/1.0.0

NOTE: this package is not compatible with Elm 0.19.1

Reflect

In your face, type system! This module provides some very un-Elmy run-type introspection on type parameters by hacking around with toString, regular expressions and Json decoders.

In general these functions are all very horrible and should never be used, but there are a few specialized cases (probably mostly based around situations where you want to maintain type-safety for stuff that is loaded at runtime) that it can prove useful.

For a real-world example of this package being used, check out the ccapndave/elm-translator package.

typeToName : a -> String

Get the base name of a type as a string, discarding any type parameters.

type Literal
  = MyDetails { age : Int, height : Int }


typeToName (MyDetails { age = 30, height = 158 }) --> "MyDetails"
typeToParameters : a -> String

Get the type parameters of a type as a string, discarding the identifier.

type Literal
  = MyDetails { age : Int, height : Int }


typeToParameters (MyDetails { age = 30, height = 158 }) --> "{ age = 30, height = 158 }"
typeParameterRecordToDict : Decoder b -> a -> Result String (Dict String b)

Turn a parameter which is a record into a Dict.

For example, if you have a type:

type Literal
  = MyDetails { age : Int, height : Int }

Then you could use this function to get the type parameter as a Dict String Int with:

typeParametersToDict Json.Decode.int <| MyDetails { age = 30, height = 158 }

Note that this will only work when there is a single type parameter which is a record, and all the elements of the record have to be of the same type. You can choose the type using the decoder parameter, but probably only simple types will work (string, int, float, boolean).

typeParameterToInt : a -> Result String Int

Turn a parameter which is a record into a Int.

For example, if you have a type:

type Literal
  = Age Int

Then you could use this function to get the type parameter as a Int with:

typeParameterToInt <| Age 30