OK, the question may be a (very)beginner one, but I'm still learning.
I have two different "ref objects"
From the "value" module:
type
Value* = ref object
case kind*: ValueKind:
of stringValue:
s*: string
of integerValue:
i*: int
of realValue:
r*: float
of booleanValue:
b*: bool
of arrayValue:
a*: seq[Value]
of dictionaryValue:
d*: Table[string,Value]
of functionValue:
f*: Function
of objectValue:
o*: Object
proc valueFromString*(v: string): Value =
new(result)
result.kind = stringValue
result.s = v
From the "argument" module:
type
Argument* = ref object
case kind*: ArgumentKind:
of identifierArgument:
i*: int
of literalArgument:
v*: Value
proc argumentFromStringLiteral(l: cstring):Argument {.exportc.} =
let ret = Argument(kind: literalArgument, v: valueFromString($l))
return ret
The thing is the compiler complains at the penultimate line (when trying to create a new Argument):
type mismatch: got <Value> but expected 'Value = ref Value:ObjectType
What am I doing wrong? The valueFromString is return a "pointer" to Value object, isn't it?
Can you provide a self-contained example? Because I tried to replicate it:
import tables
type
Function* = object
Object* = object
ValueKind* = enum stringValue, integerValue, realValue, booleanValue, arrayValue, dictionaryValue, functionValue, objectValue
Value* = ref object
case kind*: ValueKind:
of stringValue:
s*: string
of integerValue:
i*: int
of realValue:
r*: float
of booleanValue:
b*: bool
of arrayValue:
a*: seq[Value]
of dictionaryValue:
d*: Table[string,Value]
of functionValue:
f*: Function
of objectValue:
o*: Object
proc valueFromString*(v: string): Value =
new(result)
result.kind = stringValue
result.s = v
type
ArgumentKind* = enum identifierArgument, literalArgument
Argument* = ref object
case kind*: ArgumentKind:
of identifierArgument:
i*: int
of literalArgument:
v*: Value
proc argumentFromStringLiteral(l: cstring):Argument {.exportc.} =
let ret = Argument(kind: literalArgument, v: valueFromString($l))
return ret
let a = valueFromString("hello")
let t = argumentFromStringLiteral("hi")
echo repr a
echo repr t
But it works (tested on devel and Nim Playground)Thanks a lot for pointing out that it worked.
After hours of going crazy with this one, I ran your version (which seems identical to mine) and it worked. So I set out to see what was different. And spotted a ... "Value* = ref object" somewhere in my Argument object definition... (obviously left there while trying to debug sth, but quite sneaky).
Thanks ;)
UPDATE
I replace my object creation code (within valueFromString for example) with:
result = Value(kind: stringValue, s: v)
and now, everything seems to be working fine + without warnings.
Basically I was a bit hesitant with the Value(....) expression. I thought that was to be used only with "regular" non-ref objects, and that instantiating a ref object type had to go through "new", but after printing out the objects for debugging, it seems like everything is a ref, and doing its job as supposed to. So... great :)
Yeah, this behaviour is described in the manual - https://nim-lang.org/docs/manual.html#types-object-construction
"For a ref object type system.new is invoked implicitly."