如何在 Nim 中禁用警告

How to disable warning in Nim

我正在尝试抑制此警告:

Warning: inherit from a more precise exception type like ValueError, IOError or OSError. If these don't suit, inherit from CatchableError or Defect. [InheritFromException]

我试过这个:

type
  ReturnException* = ref object of Exception {.warning[InheritFromException]:off.}
  value*: BaseType

warning pragma 是一个打开和关闭的开关。所以你需要做这样的事情:

{.warning[InheritFromException]:off.}
type
  ReturnException* = ref object of Exception
  value*: BaseType
{.warning[InheritFromException]:on.}

使用这个:

type
  ReturnException* = ref object of CatchableError

或:

type
  ReturnException* = ref object of Defect

区别在于CatchableException可以被try/except捕获,而Defect总是导致程序退出。