src/skinsuit/conditional

Search:
Group by:
Source   Edit  

object variants generalized to any condition for each possible union value

currently uses {.union.} (in the future it may change), so can be buggy with memory managed types

Example:

import src/skinsuit/conditional
type Foo {.conditional.} = ref object
  num: int
  case branch: _ # type has to be _, all types without a case branch with _ as discriminator type are kept in the type section
  # the name "branch" can be changed or made _ in which case it defaults to "branch" for now
  of Odd, num mod 2 == 1: # branch names can also be _
    name: string
  of DoubleEven, num mod 4 == 0:
    a, b: int
  of Even:
    a: int # duplicate names are allowed, only the accessors care that they have the same type

var foo = Foo(num: 1)
foo.name = "abc"
doAssert foo.name == "abc"
doAssert foo.branch == Odd # branch is not an actual variable
foo.num = 2
foo.resetBranch() # advantage over object variants, named after "branch"
foo.a = 3
doAssert foo.a == 3
doAssert foo.branch == Even
foo.num = 16
doAssert foo.a == 3
foo.b = 4
doAssert foo.b == 4
doAssert foo.branch == DoubleEven
foo.resetBranch()
doAssert foo.a == 0
doAssert foo.b == 0

Types

ConditionalFieldDefect = object of FieldDefect
Source   Edit  

Macros

macro conditional(body)
Source   Edit