Types of access connections outside of the ad module in Elm

Considering

--module 1
module Module1 exposing (Message) where

type Message
  = Test String
  | Error Int


--module 2
module Module2 exposing (sayTest, sayError) where

import Module1 exposing (Message)

sayTest : String -> Message
sayTest msg =
  Test msg  --error

sayError : Int -> Message
sayError code =
  Error code --error


processMessage : Message -> String
processMessage msg ->
  case msg of
    Test s -> s
    Error i -> toString i

How do I access Testand Errorfrom module 2?

At the moment, I need to create functions in module 1 that, when called, will create the necessary instances, but as the list gets longer, it becomes impractical.

+4
source share
1 answer

You can open all types of constructors for the exported type as follows:

module Module1 (Message (..)) where

In addition, if you want to export only a few type constructors, you can call them separately:

module Module1 (Message (Test, Error)) where

type Message
  = Test String
  | Error Int
  | Foo String
  | Bar

In the above code, the Foo and Bar constructors remain private to the module.

+5
source

Source: https://habr.com/ru/post/1625226/


All Articles