How to get a particular key value pair from elm object

Viewed 166

I have a elm object which returns value are as follows

type alias Info =
    { name : String
    , stdId : stdKey
    , pemId : PermanentKey
    }

This info lies in a page named as class.elm

in another page i want to use the std key alone to do a if else comparison. i tied to assign the stdKey to a variable like as follows

     uniKey = class.Info.stdId

But the elm doesn't accept this way . Kindly help.

2 Answers

Assuming you intend to create a Info value in the Class.elm file and use it in an Other.elm file :

file Class.elm

module Class exposing (Info) 

type alias Info =
    { name : String
    , stdId : StdKey
    , pemId : PermanentKey
    }

type alias StdKey = String
type alias PermanentKey = String

info : Info
info = 
   { name = "name"
   , stdKey = "valueForStdKey"
   , pemKey = "valueForPemKey"
   } 



file Other.elm

module Other exposing (..)

import Class

uniKey : Class.StdKey
uniKey = Class.info.stdKey

Alternative file Alt.elm

module Alt exposing (..)

import Class exposing (StdKey, PermanentKey, info)

uniKey : StdKey
uniKey = info.stdKey


Elm automatically generates a .stdId function that works on any record with that field

.stdId info == info.stdId
Related