小编Bal*_*azs的帖子

如何在haskell中仅导入特定实例

我面临以下问题:我在一个模块中定义了一个类型类的多个实例,但不想导出所有这些实例。我怎样才能做到这一点?

module SomeModule
  ( calculate
  )
where

class Something a where
  calculate :: a -> Int

instance Something Double where
  calculate x = calculate $ roundToInt x

instance Something Int where
  calculate x = doSomething x

roundToInt :: Double -> Int
roundToInt = round

doSomething :: Int -> Int
doSomething _ = 42
Run Code Online (Sandbox Code Playgroud)

在这个(简化的)示例中,我有两个Something相互依赖的类型类实例,但我只想导出Doubles 而不是Ints的实例。但在我的示例中,两个实例都被隐式导出。有办法解决吗?

haskell module export instance typeclass

1
推荐指数
1
解决办法
70
查看次数

标签 统计

export ×1

haskell ×1

instance ×1

module ×1

typeclass ×1