如何从实例中调用不带参数的函数?

假设你有一个类:

class AClass a where
  func:: Int

instance AClass SomeTree where
  func = 0

instance AClass Double where
  func = 1

我如何调用函数func?

回答

{-# LANGUAGE AllowAmbiguousTypes, TypeApplications #-}

class AClass a where
  func :: Int

instance AClass SomeTree where
  func = 0

instance AClass Double where
  func = 1

foo :: Int
foo = func @SomeTree + func @Double

{-# LANGUAGE ScopedTypeVariables, UnicodeSyntax #-}

bar :: ? a . AClass a => a -> Int
bar _ = func @a


以上是如何从实例中调用不带参数的函数?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>