I did not dive deeply into concepts as they are not yet "stable" but I think 
this shows a bit how simple generics and concepts differ.
    
    
    # a simple concept example
    
    type Countable = concept c
      inc c
      dec c
    
    type Foo = object
      v: int
      s: string
    
    proc inc(a: var Foo) =
      a.v += 1
    
    proc dec(a: var Foo) =
      a.v -= 1
    
    proc twoForOneBackConcept(a: Countable): Countable =
      result = a
      inc result
      inc result
      dec result
    
    proc twoForOneBackGeneric[T](a: T): T =
      result = a
      inc result
      inc result
      dec result
    
    echo twoForOneBackConcept(1)
    echo twoForOneBackGeneric(1)
    echo twoForOneBackConcept('a')
    echo twoForOneBackGeneric('a')
    
    var x = Foo(v: 4, s: "Test")
    echo twoForOneBackConcept(x)
    echo twoForOneBackGeneric(x)
    
    when false:
      echo twoForOneBackConcept("Test") # will fail in this line
      echo twoForOneBackGeneric("Test") # will fail at "inc" in the proc
    

Reply via email to