如何从实例中调用不带参数的函数?
假设你有一个类:
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