I am trying to get some thing like this to work, but I am having issues with 
arrays and the type system:

Working code, but I have to write array[16, char] which is awkward: 
    
    
    proc pack[A](str: string): A =
      if str.len > result.len:
        raise newException(ValueError, "Can't pack " & $str.len & " string into 
" & $result.len)
      for i in 0..<result.len:
        if i >= str.len:
          result[i] = char(0)
        else:
          result[i] = str[i]
    
    
    # example struct
    type Test = object
      x: int32
      y: int32
      name: array[16, char]
      life: float32
    
    
    var test: Test
    test.name = pack[array[16, char]]("supernamething")
    
    echo sizeOf(Test)
    

I am trying to wrap that as a type
    
    
    type PackedString[N] = array[N, char]
    
    
    proc pack[N](str: string): PackedString[N] =
      if str.len > result.len:
        raise newException(ValueError, "Can't pack " & $str.len & " string into 
" & $result.len)
      for i in 0..<result.len:
        if i >= str.len:
          result[i] = char(0)
        else:
          result[i] = str[i]
    
    
    # example struct
    type Test = object
      x: int32
      y: int32
      name: PackedString[16]
      life: float32
    
    
    var test: Test
    test.name = pack[16]("supernamething")
    
    echo sizeOf(Test)
    

Can some one tell me what I am doing wrong?

Thanks! 

Reply via email to