How to relate to type from outer context

后端 未结 2 1532
太阳男子
太阳男子 2020-12-29 02:21

Let us consider the following code snippet:

blah :: a -> b -> a
blah x y = ble x where
    ble :: b -> b
    ble x = x

This compil

2条回答
  •  心在旅途
    2020-12-29 03:04

    If you don't want to use ScopedTypeVariables, you can use the good ole fashion asTypeOf function.

    -- defined in Prelude
    asTypeOf :: a -> a -> a
    x `asTypeOf` y = x
    

    blah :: a -> b -> a
    blah x y = ble x where
        ble x = x `asTypeOf` y
    

    Of course, this won't compile because of the type error.

    Update:

    I would like to point out that sometimes you might have to be a little crafty to do what you want with asTypeOf. Take the following example that superflously uses asTypeOf because I don't want to think of a case that actually needs asTypeOf. Similar solutions would work the same for real world cases.

    foo :: Bounded a => Maybe a -> a
    foo m = x
      where
        x = maxBound -- Q: how do I make (x :: a) when given (Maybe a)?
        _ = Just x `asTypeof` m -- A: witchcraft!
    

提交回复
热议问题