代码之家  ›  专栏  ›  技术社区  ›  user6023611

为什么编译器对同构类型感到愤怒?

  •  1
  • user6023611  · 技术社区  · 9 年前
    Expected type: ErrorT String IO Integer
    Actual type: IO (Either String Integer)
    

    我不知道我错在哪里。毕竟,这些类型是同构的。
    当然,当我从

    ErrorT String IO Integer  
    

    IO (Either String Integer)
    

    编译器很高兴,但我不高兴,因为我很困惑。

    1 回复  |  直到 9 年前
        1
  •  4
  •   Cactus    9 年前

    回答这个问题有点棘手,因为没有真正的问题,但编译器不高兴,因为类型可能是 同构的 但它们不是同义词 ErrorT 这样:

    newtype ErrorT e m a = ErrorT m (Either e a)
                           ^^^^^^
    

    see here

    你已经在你的另一个问题中提到了这一点-这是像

    return (Left "Hello") :: IO (Either String Integer)
    

    ErrorT (return (Left "Hello")) :: ErrorT String IO Integer