Hello, you might be looking for "extensible records", check this out 
http://elm-lang.org/docs/records

import Html exposing (Html, div, ul, li, text)

type alias Person a =
    { a | name : String
    , address : String
    }

type alias Employee a =
    { a | department : String }

type alias Customer a =
    { a | itemsPurchased : Int }

changeName : String -> Person a -> Person a
changeName newName person =
    { person | name = newName }

changeDepartment : String -> Employee a -> Employee a
changeDepartment newDepartment employee =
   { employee | department = newDepartment }

changeItemsPurchased : Int -> Customer a -> Customer a
changeItemsPurchased newItemsPurchased customer =
    { customer | itemsPurchased = newItemsPurchased }

main =
    let
        person = { name="person name", address="person address"}
        employee = { name="employee name", address="employee address", 
department = "employee department" }
        customer = { name="customer name", address="customer address", 
itemsPurchased = 42 }
    in
    div [] 
        [ ul []
            [ renderIt person
            , renderIt employee
            , renderIt customer
            , renderIt (changeName "name changed" person)
            , renderIt (changeName "name changed" employee)
            , renderIt (changeName "name changed" customer)
            
            -- , renderIt (changchangeDepartmenteName "department changed" 
person) 
            , renderIt (changeDepartment "department changed" employee)
            -- , renderIt (changeDepartment "department changed" customer)

            -- , renderIt (changeItemsPurchased 55 person)
            -- , renderIt (changeItemsPurchased 55 employee)
            , renderIt (changeItemsPurchased 55 customer)
            ]
        ]


renderIt : a -> Html b
renderIt r =
    li [] [text (toString r)]





On Monday, July 18, 2016 at 7:06:32 AM UTC+8, Leonardo Sá wrote:
>
> Short question:
>
> What is the best way to create relationships between my types so it is 
> easy to access data present in both types?
>
> Long question:
>
> Suppose I have the following types:
>
> type alias Person =
>   { name : String
>   , address : String
>   , personType : PersonType 
>   }
>
> type alias Employee =
>   { department : String }
>
> type alias Customer =
>   { itemsPurchased : Int }
>
> type PersonType = EmployeeType Employee | CustomerType Customer
>
> Then I think it's not straight forward to write a function that retrieves 
> me the department and name for an employee:
>
> nameAndDepartment : Person -> (String, String)
>
> It seems to me this function would be a Maybe (String, String) and return 
> Nothing if the Person is not an Employee. But in that case, I am relying on 
> the runtime to type check things for me, which tells me there is probably a 
> better way to structure this.
>
>

-- 
You received this message because you are subscribed to the Google Groups "Elm 
Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to elm-discuss+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to