Skip to content

Instantly share code, notes, and snippets.

@dustinlacewell
Last active April 5, 2017 01:35
Show Gist options
  • Save dustinlacewell/6b97b658c2db44e1b22fa021873fe133 to your computer and use it in GitHub Desktop.
Save dustinlacewell/6b97b658c2db44e1b22fa021873fe133 to your computer and use it in GitHub Desktop.
type # concepts
Value[T] = concept v
v.value is T
MinValue[T] = concept v
v is Value
v.minVal is T
MaxValue[T] = concept v
v is Value
v.maxVal is T
RangedValue[T] = concept v
v is MinValue
v is MaxValue
type # implementations
BoolParam = ref object
value: bool
FloatParam = ref object
minVal, maxVal: float
value: float
IntParam = ref object
minVal, maxVal: int
value: int
StringParam = ref object
value: string
NoteParam = ref object
minVal: int
maxVal: int
value: int
octave: int
# generic set will produce multiple implementations
proc set[T](self: Value, v: T): bool =
# following block only compiled when expr is true
when self is MinValue:
if v < self.minVal: return false
when self is MaxValue:
if v > self.maxVal: return false
self.value = v
return true
proc `$` (self: Value): string =
return $(self.value)
proc `$` (self: NoteParam): string =
return "$1:$2" % [$self.value, $self.octave]
import unittest
include main
suite "synth tests":
test "concept validation":
# checking whether our types are valid for the concepts
check(BoolParam is Value)
check(NoteParam is RangedValue)
check(FloatParam is RangedValue)
test "BoolParam":
let b = BoolParam(value: false)
discard b.set(true)
assert b.value
test "FloatParam":
let f = FloatParam(minVal:0.0, maxVal:1000.0, value: 0.0)
discard f.set(100.0)
discard f.set(10001.0)
assert f.value == 100.0
test "NoteParam":
let n = NoteParam(minVal:0, maxVal: 12, value:4, octave: 6)
discard n.set(8)
discard n.set(-1)
assert n.value == 8
echo $n
assert $n == "8:6"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment