I have a question. In the following code, the converter with static int 
generics fails. Are there any way to avoid this?

My purpose is to make C++-like constructor for which calling <Classname>(value) 
generates S object because the conventional constructor using init*** is 
inconvenient in programming with generics.

I know that this is possible by using converter. Are there any other way to 
make this kind of constructor?
    
    
    type S[N:static[int]] = object
      a:int
    
    converter toS[N:static[int]](a:int):S[N] = S[N](a:a mod N)
    
    echo toS[13](19) # works
    #var x:S[13] = 19 # Error: type mismatch: got <int literal(19)> but 
expected 'S[13]'
    #echo S[13](19) # Error: type mismatch: got <int literal(19)> but expected 
'S[13]'
    
    
    type S2[T] = object
      a:T
    
    converter toS2[T](a:T):S2[T] = S2[T](a:a)
    
    echo toS2(21) # works
    var y:S2[int] = 21 # works
    echo S2(21) # works
    
    
    Run

Reply via email to