Thanks for the reply. I tried to apply that to a simple Java interface ( 
Comparable<T>) and came up with this:
    
    
    type
      Comparable[T] = object
        compareTo : proc (other: T) : int
      Car = object
        horsepower : int
        model : string
      School = object
        num_students : int
        name : string
    
    proc GetComparable(this: Car): Comparable[Car] =
      return Comparable[Car](
      compareTo : proc (other : Car): int =
         if this.horsepower == other.horsepower:
           result = 0
         elif this.horsepower < other.horsepower:
           result = -1
         else:
           result = 1
      )
    
    proc GetComparable(this: School): Comparable[School] =
      return Comparable[School](
      compareTo : proc (other : School): int =
         if this.num_students == other.num_students:
           result = 0
         elif this.num_students < other.num_students:
           result = -1
         else:
           result = 1
      )
    
    proc Compare[T](first: Comparable[T], second: T) : int =
      return first.compareTo(second)
    
    
    let car1 = Car(horsepower: 400,model: "Mustang")
    let car2 = Car(horsepower: 350,model: "Camaro")
    let car3 = Car(horsepower: 400,model: "Camaro XL")
    
    let icar1 = GetComparable(car1)
    let icar2 = GetComparable(car2)
    
    let school1 = School(num_students: 10000, name: "ISU")
    let school2 = School(num_students: 12000, name: "OSU")
    
    let ischool1 = GetComparable(school1)
    
    proc ReportResults(return_code : int)  =
      if return_code == 0:
        echo "values were equal"
      elif return_code == -1:
        echo "first value lower than second"
      else:
        echo "first value higher than second"
    
    var return_code = Compare[Car](icar1,car2)
    ReportResults(return_code)
    return_code = Compare[Car](icar2,car1)
    ReportResults(return_code)
    return_code = Compare[Car](icar1,car3)
    ReportResults(return_code)
    return_code = Compare[School](ischool1, school2)
    ReportResults(return_code)
    
    
    
    Run

Reply via email to