Any problem on Earth can be solved with the careful application of 
meta-programming. The trick is not to be around when the AST spec changes. :o)
    
    
    import macros
    
    type Range[V] = object
      v: V
      slice: Slice[int]
    
    var r: Range[seq[int]] = Range[seq[int]](v: @[0, 10, 20, 30, 40], slice: 
2..3)
    # r represents the range [20, 30]
    
    var x : r.v[0].type # ok: r.v[0].type is int,  the type of the elements, so 
x is an int
    
    macro elemType(t: typed): untyped =
      t.getTypeImpl[1][1].copy
    
    #If I want a generic proc to get the first element of a Range:
    proc first[V](r: Range[V]): V.elemType =
      return r.v[r.slice.a]
    
    echo first(r) # Would be 20
    
    
    Run

Reply via email to