Hacker News new | past | comments | ask | show | jobs | submit login

At least in the devel branch type classes seem to be working:

  type Comparable = generic x, y
    (x < y) is bool
This creates a Comparable typeclass, which can then be instantiated:

  type Foo = tuple[a, b: int]

  proc `<`(x, y: Foo): bool =
    if    x.a < y.a: true
    elif  x.a > y.a: false
    else: x.b < y.b

  var c: Foo = (12, 13)
  var d: Foo = (14, 15)
  echo c < d # true
And used elsewhere in generic code:

  proc min[T: Comparable](xs: openarray[T]): T =
    assert xs.len > 0
    result = xs[0]
    for x in xs:
      if x < result:
        result = x

  echo min([c,d]) # (a: 12, b: 13)



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: