nim-lang / RFCs

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

support `when` in case statements #274

Closed timotheecour closed 3 years ago

timotheecour commented 3 years ago

proposal

allow when in case statements

example

type Foo = enum f0 f1
proc main()=
  var f = f0
  case f
  of f0: echo "ok0"
  of f1: echo "ok1"
  when defined(case2_1):
    of f2:
      echo "ok2"
  elif defined case2_2:
    of f2b, f2c:
      echo "ok2b"
  elif defined case2_3:
    else: # else is allowed too
      echo "ok3"
main()

likewise with case objects:

type Foo = enum f0 f1
type Bar = object
  case f: Foo
  of f0: v0: int
  of f1: v1: int
  when not defined(js):
    of f3: v3: int

implementation

inside a when statement (or elif branch following a when statement), a of or else shall be attached to preceding case statement.

related

mratsim commented 3 years ago

Is there a reason for closing this RFC? Change of mind?

timotheecour commented 3 years ago

Wasnt complete yet, I've writing a library solution for this and will compare to a compiler solution, more details soon