Hello, As a new user I'm having problems passing an object ref to a proc. Any guidance on the code problem will be greatly appreciated.
My version of nim is the following:
Nim Compiler Version 0.11.0 (2015-05-01) [Linux: amd64] Copyright (c) 2006-2015 by Andreas Rumpf git hash: 0be654efe11311fcf1200c0e7dba9ecd55fa5591 active boot switches: -d:release
A contrived example cut down from my application code is the following:
type Pwlf = object of RootObj
monotonic: int
domain: array[2,float]
range: array[2,float]
domainList: seq[float]
rangeList: seq[float]
proc newPwlf[T](domList: seq[T], rngList: seq[T]): ref Pwlf =
new(result)
result.domainList = domList
result.rangeList = rngList
proc fooOp(x: ref Pwlf, y: ref Pwlf): ref Pwlf =
new(result)
result.rangeList = y.rangeList
return result
var
list: seq[int]
domList: seq[float]
rngList: seq[float]
domList = @[0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0]
rngList = @[0.0,0.5,1.0,1.5,2.0,2.5,3.0,3.5,4.0,4.5]
var foo: ref Pwlf = newPwlf(domList,rngList)
var bar: ref Pwlf = newPwlf(domList,rngList)
echo fooOp(foo,bar)
The error message is the following:
befuddled.nim(34, 11) Error: type mismatch: got (ref Pwlf) but expected one of: system.$(x: int64) system.$(x: float) system.$(x: seq[T]) system.$(x: TEnum) system.$(x: bool) system.$(x: T) system.$(x: uint64) system.$(x: set[T]) system.$(x: int) system.$(x: string) system.$(x: char) system.$(x: cstring)
For echo to work with user-defined types, you need to define the $ operator for them.
Example:
proc `$`(x: ref Pwlf): string =
repr(x)
Hello, Thanks for finding my fubar.
If take the repr() of the fooOp output, the program completes as expected. I will define a $ operator in the future.
echo repr(fooOp(foo,bar)
Thanks again for the assistance!
Hello, As a followup to my previous question, I'm facing an issue where I cannot add to a seq declared in a parameterized proc. The cut down code to isolate the issue is below:
type Pwlf[T] = object of RootObj
monotonic: int
domain: array[2,T]
range: array[2,T]
domainList: seq[T]
rangeList: seq[T]
proc newPwlf[T](domList: seq[T], rngList: seq[T]): ref Pwlf[T] =
new(result)
result.domainList = domList
result.rangeList = rngList
proc mergeOp[T](this: ref Pwlf[T], other: ref Pwlf[T], op: proc (x: T, y: T): T {.closure.}): ref Pwlf[T] =
var resRangeList = @[T]
for i in 0..<len(this.domainList):
resRangeList.add(other.rangeList[i])
result = newPwlf(other.domainList,resRangeList)
proc addWrap[T](this: T, other: T): T =
result = system.`+`(this,other)
proc `$`(x: ref Pwlf): string =
repr(x)
var
list: seq[int]
domList: seq[float]
rngList: seq[float]
domList = @[0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0]
rngList = @[0.0,0.5,1.0,1.5,2.0,2.5,3.0,3.5,4.0,4.5]
var foo: ref Pwlf[float] = newPwlf(domList,rngList)
var bar: ref Pwlf[float] = newPwlf(domList,rngList)
echo mergeOp(foo,bar,addWrap)
The error message suggests some sort of problem with the parameterized times given the reference to typdedesc[float] that my experimentation has been unable to circumvent so far. The error message is as follows:
clueless.nim(46, 13) Info: template/generic instantiation from here clueless.nim(22, 17) Error: type mismatch: got (seq[typedesc[float]], float) but expected one of: system.add(x: var seq[T], y: T) system.add(x: var string, y: char) system.add(x: var string, y: string) system.add(x: var seq[T], y: openarray[T]) system.add(x: var string, y: cstring)
Use newSeq[T]() instead of @[T]. You're constructing a sequence that contains the type T as its only element, and not a sequence of instances of T.
Alternatively, the following also works:
var resRangeList: seq[T] = @[]