I am trying to use template with two different proc like below,
    
    
    
    type
        Person = object
            name: string
            job: string
            email: string
    
    var jerry = Person(name: "JK", job: "engineer", email:"N/A")
    
    proc printA[T](x: T) =
        echo x
    
    proc printB[T](x: T) =
        for field, value in x.fieldPairs:
            echo field, value
    
    
    Run
    
    
    template printIt*[T](x: T) =
        let o = astToStr(x).split(".")
        case o.len:
        of 2 :
            x.printA()
        else:
            discard
    
    
    
    Run

the above code works , but when I try below, it failed.
    
    
    template printIt*[T](x: T, dir: string) =
        let o = astToStr(x).split(".")
        case o.len:
        of 2 :
            x.printA()
        of 1 :
            x.printB()
        else:
            discard
    
    
    Run

here is test code and error output
    
    
    jerry.printIt(). # len == 1, should call x.printB()
    jerry.job.printIt() # len == 2, should call x.printA()
    
    
    Run
    
    
    template/generic instantiation of `printIt` from here
    template/generic instantiation of `printB` from here
    Error: type mismatch
    Expression: fieldPairs(x)
      [1] x: string
    
    Expected one of (first mismatch at [position]):
    [1] iterator fieldPairs[S: tuple | object; T: tuple | object](x: S; y: T): 
tuple[
        key: string, a, b: RootObj]
    [1] iterator fieldPairs[T: tuple | object](x: T): tuple[key: string, val: 
RootObj]
    
    
    
    Run

Reply via email to