timotheecour / Nim

Nim is a compiled, garbage-collected systems programming language with a design that focuses on efficiency, expressiveness, and elegance (in that order of priority).
http://nim-lang.org/
Other
2 stars 0 forks source link

common review comments #534

Open timotheecour opened 3 years ago

timotheecour commented 3 years ago

links

TODO

timotheecour commented 3 years ago
func builtin_rotl8(value: cuchar, shift: cint): cuchar {.importc: "__rolb", header: "x86intrin.h".}
=>
func builtin_rotl8(value: cuchar, shift: cint): cuchar {.importc: "__rolb", header: "<x86intrin.h>".}
timotheecour commented 3 years ago
timotheecour commented 3 years ago

all tests run under a single main template:

discard """
  targets: "c js"
"""

template main =
  block: # foo
     doAssert foo(1) == 1
     doAssert foo(1, 2) == 3

  block: # bar
     doAssert bar()
     doAssert not compiles(bar("abc"))

  block: # bug #1234
    ...

static: main()
main()
timotheecour commented 3 years ago

block foo: should only be used if the code refers to foo as a block label (eg via break foo); nim might in future issue warnings for labels without code referring to it

timotheecour commented 3 years ago

inline definitions of variables used only once, eg:

    let
      a = some(42)
      b = none(int)
      c = some(-11)
    assert a.flatMap(doublePositives) == some(84)
    assert b.flatMap(doublePositives) == none(int)
    assert c.flatMap(doublePositives) == none(int)

=>

    assert some(42).flatMap(doublePositives) == some(84)
    assert none(int).flatMap(doublePositives) == none(int)
    assert some(-11).flatMap(doublePositives) == none(int)

it results in more readable tests where all the context is in 1 line

timotheecour commented 3 years ago

function tested goes on LHS for clarity:

    assert {true, false} == fullSet(bool)
=>
    assert fullSet(bool) == {true, false}
timotheecour commented 3 years ago

the 1st form shows:

/t11954.nim(13, 10) `bounds.a <= bounds.b`  bounds b should be larger or equal to bounds.b [AssertionDefect]

the 2nd shows:

t11954.nim(13, 10) `bounds.a <= bounds.b`  [AssertionDefect]

which has exactly the same information. Simpler is better, both for reading and writing code and for developpers who will encounter this error msg.

note: runtime context

Currently assert + friends (unlike unittest.check) doesn't show runtime values on an assertion failure, but in future this could be implemented at least for the most common cases, controlled by a flag, maybe --errorcontexts:on.

Until then, to show runtime values on errors, we need:

assert bounds.a <= bounds.b, $(bounds.a, bounds.b)

this shows:

t11954.nim(13, 10) `bounds.a <= bounds.b` (4, 3) [AssertionDefect]

which is IMO perfectly readable, especially if this becomes a pattern; simplicity wins here. We could also introduce a reusable helper macro to format those, eg:

assert bounds.a <= bounds.b, prettyMsg(bounds.a, bounds.b)

which would then show:

t11954.nim(13, 10) `bounds.a <= bounds.b` (bounds.a: 4, bounds.b: 3) [AssertionDefect]
timotheecour commented 3 years ago

refs https://github.com/nim-lang/Nim/pull/14327, https://github.com/nim-lang/Nim/pull/17559

timotheecour commented 3 years ago

either read github docs on how to do this, or do the following to avoid lots of conflicts:

before changing back target branch to devel, do this:

timotheecour commented 3 years ago

rationale: