nim-lang / RFCs

A repository for your Nim proposals.
136 stars 26 forks source link

Automatically import std/assertions in runnableExamples #499

Closed metagn closed 1 year ago

metagn commented 1 year ago

Abstract

Automatically import std/assertions in runnableExamples blocks.

Motivation

Pretty much every runnableExamples block has an assert statement. Would save hundreds of lines of code to not have to explicitly import it in every single one.

Description

Seems pretty self explanatory. Don't know about implementation details.

Code Examples

runnableExamples:
  assert true # works

Backwards Compatibility

No response

juancarlospaco commented 1 year ago

Workaround runnableExamples"--import:std/assertions":.

ringabout commented 1 year ago

-d:nimPreviewSlimSystem hasn't been enabled, so I'm not sure whether we should enforce the option for the runnableExamples. Otherwise, a define should be generated instead

when defined(nimPreviewSlimSystem):
  ...
metagn commented 1 year ago

Then we would get "warning doAssert is about to move out of system". IMO instead of when defined(nimPreviewSlimSystem): import ... we should use NimMajor >= 2 or defined(nimHasSlimSystemModules). Otherwise insane amounts of code will have a dependency on a "preview" define, meant to be phased out.

(Or add std/fatsystem which exports all of them and check (compiles do: import std/fatsystem))

(Or make unknown imports a warningAsError by default and do {.push warning[UnknownImport]: off.}; import std/assertions; {.pop.} which can be used universally for new modules)