我希望在Haskell中有一个重载函数。
{-# LANGUAGE FlexibleInstances #-}
class Foo a where
foo :: a
instance Foo (String -> Int) where
foo = length
instance Foo String where
foo = "world"
然而,这种过载对于类型模糊性处理非常糟糕。print $ foo "hello"
将导致错误,而print $ length "hello"
却可以正常工作。但是,只要我的实例列表固定,Haskell就没有技术上的障碍来认识到foo :: String -> a
的唯一实例是foo :: String -> Int
。我能让Haskell做出这个认识吗?
fooLen
和fooStr
,但我认为重载更加方便。 - Karolis JuodelėprintFoo
),然后如果有人使用它并添加了一个实例instance Foo (String -> Double)
,那么他会遇到麻烦。 - Random Dev