Can I export constructors along with a type alias?

前端 未结 3 1067
天命终不由人
天命终不由人 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:48

    Why do you want to do this? No, don't bother answering. Don't do this. Whatever you think it will accomplish, it will not. What you can do, however, is use a newtype (note that I changed the names a bit):

    newtype Bar = _Bar (Foo Int Int)
    data Foo a b = Foo a b
    

    Now you can use pattern synonyms to make Bar user-friendly:

    {-# LANGUAGE PatternSynonyms #-}
    
    module Whatever (Bar, pattern Bar)
    pattern Bar a b = _Bar (Foo a b)
    

    There's a bit of weirdness having to use the pattern keyword to import the synonym, but oh well. Unlike your approach, the end user has access to a proper first-class type and not just a synonym. And they can't see anything of Foo.

提交回复
热议问题