How to disambiguate selector function?

后端 未结 1 1996
广开言路
广开言路 2021-01-02 00:33

In GHC 8:

{-# LANGUAGE DuplicateRecordFields #-}

data Dog = Dog { name::String }
data Human = Human { name::String }

dog = Dog \"Spike\"


main = putStrLn          


        
相关标签:
1条回答
  • 2021-01-02 00:39

    this should work:

    main = putStrLn $ name (dog :: Dog)
    

    see DuplicateRecordFields for details:

    Bare uses of the field refer only to the selector function, and work only if this is unambiguous.

    and

    However, we do not infer the type of the argument to determine the datatype, or have any way of deferring the choice to the constraint solver.

    The example there is very much like yours:

    bad (p :: Person) = personId p

    this will not work when there is another record with a personId field in scope - even if it seems to be obvious :(

    0 讨论(0)
提交回复
热议问题