I have defined an enum type like:
type
myEnum = enum
someValue = "str-Value"
anotherValue = "another-Value"
with a $ overload like:
func `$`*(v: myEnum): string =
case v:
of someValue: return "one"
of anotherValue: return "two
And all this residing in one distinct module.
(obviously, the above is just an example; albeit an accurate one...)
The question is two-fold:
One:
Let's say I import the module in a different one... shouldn't every $(something-that-is-myEnum) always call the right $ overload? (the one defined above)
At present, it feels mostly like random (basically, I have to try it every single time to make sure it doesn't output someValue & anotherValue... that is: totally ignoring my $ function.
Two:
Based on the above, although a tiny bit unrelated, in the generated C code (and in the binary as well, obviously) all this string values are there (someValue, anotherValue)... What can I do to complete get rid of them for selected enums?
If your overload is in scope it will call your procedure as it's the most specific. Now this is not true if a generic procedure bind s shut a procedure:
proc doThing(a: auto) =
bind `$`
echo $a
type A = object
proc `$`(a: A): string = "Hello"
doThing(A())
My main case is an enum value inside an object.
Both the object and the enum have $ overloads.
Generally, it works as I expect it to. Until it doesn't... (as for bind, I've never ever used it; I'm not sure I fully understand your point...)
I'll show you the example in question...
Here we have the printable module, which comes with a $ overload which is supposed to take Value objects and return them stringified.
As you can see there are different kinds of Value objects, so the stringification process varies accordingly.
Let's take 2 concrete examples:
Logical values are actually an enum and are defined here: https://github.com/arturo-lang/arturo/blob/symbol-values-as-a-distinct-module/src/vm/values/custom/vlogical.nim#L47-L50 (with their own $ overload)
And handled here: https://github.com/arturo-lang/arturo/blob/symbol-values-as-a-distinct-module/src/vm/values/printable.nim#L40
It works fine.
Symbol values are also an enum and are defined here: Logical* values are actually an enum and are defined here: https://github.com/arturo-lang/arturo/blob/symbol-values-as-a-distinct-module/src/vm/values/custom/vsymbol.nim#L114-L207 (again with their own $ overload)
And handled here: https://github.com/arturo-lang/arturo/blob/symbol-values-as-a-distinct-module/src/vm/values/printable.nim#L74-L76
Suprisingly, this does not work.
If you're wondering where Value's are defined, here it is as well: https://github.com/arturo-lang/arturo/blob/symbol-values-as-a-distinct-module/src/vm/values/types.nim#L130-L225
Still, to me this looks totally baffling. The two types are defined in an identical way, with similar overloads, used in pretty much the same way. One works, the other doesn't.
Ideas?
Jesus christ! I forgot to make the $ overload public... How stupid! (I re-organized the code/modules a bit and that's the side effect :S)
(I have had such issues in the past and I thought it was something else; in this case, it was just me. Jesus!)
Answering my own case, in the example above to avoid the default strings for the enum, all I did (and it did work) was to directly assign the string values (inside the enum definition) and avoid the $ overload altogether.
Quite an obvious solution, I guess... :)