News for hackers. Stuff that matters.
Re:Nimrod by Example by def- on Thursday July 17, 2014 @02:42PM (#8049356)
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)

There are no comments on this comment yet.