Erèbe
Erèbe

Reputation: 343

Create data type with types from imported modules

Let say we have a module A and a module B where type A and B are declared. In a module Super we want to say that Type Super is either A or B like in the following code :

-- module A
data A = A String

-- module B
data B = B Int

-- module Super
data Super = A.A
           | B.B

but this does not work as we cannot use full qualified data Type in the data constructor (the error is: "Qualified name in binding position") and if we do not put full qualified name like below, type A and B are not whose from module A and B

-- module Super
data Super = A
           | B

As far my solution is to do like this

-- module Super
data Super = A A.A
           | B B.B

Is there a better way to achieve this ? I am not sure if I have to use type class

P.s: My goal is to have a list of type Super

let mySuperList = [ B, A ] :: [Super]

Upvotes: 2

Views: 121

Answers (1)

J. Abrahamson
J. Abrahamson

Reputation: 74354

let mySuperList = [ Left B, Right A ] :: [Either B A]

If you can get away without undue multiplication of names, you'll probably find it more powerful in the long run.

Upvotes: 4

Related Questions