首页 > 解决方案 > 在不设置 -Wunused-top-binds 的情况下,推荐使用模块私有记录类型的方法是什么?

问题描述

这个模块

module Foo (Foo, qux) where

data Foo = Foo {bla::Int}

qux :: Foo
qux = Foo 37

编译时会导致警告-Wall

/tmp/wtmpf-file12937.hs:3:17: warning: [-Wunused-top-binds]
    Defined but not used: ‘bla’
  |
3 | data Foo = Foo {bla::Int}
  |                 ^^^

好的——如果bla只是一个独立的功能,这很容易,应该通过删除bla. 但作为记录,这些字段不仅仅提供了一个可以使用的名称,它们还用作代码中的文档。

摆脱警告的首选方法是什么?

它应该是一个永久的解决方案,最好保持记录原样,最好不要禁用模块其余部分的任何警告。

标签: haskellrecordcompiler-warnings

解决方案


为了避免这些,我通常在模块中添加这样的定义:

_unused :: a
_unused = error "don't complain" bla

好消息是您可以将它们链接起来,如下所示:

_unused :: a
_unused = error "don't complain" bla bah foo bar

这有点粗糙,但可以完成工作。


推荐阅读