Can I export constructors along with a type alias?

前端 未结 3 1073
天命终不由人
天命终不由人 2021-01-13 15:06

I have a data type data Foo a b = Bar a b that I use internally in a library.

I also have an alias for one of its more common concrete forms: type

3条回答
  •  悲哀的现实
    2021-01-13 15:38

    You can export Bar type simply by module Quux (Bar) where ..., though you will not be able to construct any Bars.

    If you also need the constructor, you may use similar technique to smart constructors, that is create a helper functions which creates Bars and export that one:

    module Quux (Bar, bar) where
    
    data Foo a b = Foo a b
    type Bar = Foo Int Int
    
    bar :: Int -> Int -> Bar
    bar = Foo
    

    then

    \> let b = bar 1 2
    \> :type b
    b :: Bar
    \> let f = Foo 1 2
    
    :9:9: Not in scope: data constructor ‘Foo’
    

提交回复
热议问题