All of this seems to work (except the message-queues), at least in the "inline" tests writen with "when isMainModule: ..."
My issue is, that when I use the "type register" in the message-queues, suddenly the "shared heap table" cannot find the hash function of the "shared heap string". The code in the message queues calls the same procs as the test code in the "type register", so I see no reason why the hash() proc could not be found.
The whole code is rather large now, mostly due to containing a clone of "tables.nim", so I don't want to post it here, but I'll create some github repo(s) for it, once it has been cleaned up. I'm only posting the (simplified) "string-in-heap" module for now, to show how the hash() proc is defined.
Do I need to do anything more, so that a module that receives the SharedText as generic parameter can find the hash(SharedText) proc?
import hashes
type
SharedText* = object
txt*: cstring
txthash*: Hash
txtlen*: int
proc hash*(st: SharedText): Hash {.inline, noSideEffect.} =
result = st.txthash
proc len*(st: SharedText): int {.inline, noSideEffect.} =
result = st.txtlen
proc `$`*(st: SharedText): string {.inline.} =
result = $st.txt
proc `==`*(a, b: SharedText): bool {.inline, noSideEffect.} =
(a.txthash == b.txthash) and (a.len == b.len) and (cmp(a.txt, b.txt) == 0)
proc initSharedText*(s: cstring): SharedText {.inline, noSideEffect.} =
result.txt = s
result.txtlen = len(s)
result.txthash = hash(s)
And the compiler error message looks something like:
# ...
mysharedtable.nim(153, 12) Error: type mismatch: got (SharedText)
but expected one of:
proc hash(sBuf: string; sPos, ePos: int): Hash
proc hash(x: uint64): Hash
# ...
Can you expand on:
module that receives the SharedText as generic parameter can find the hash(SharedText) proc?
What do you mean exactly? Does the module import your SharedText module? If so, it should find your hash(SharedText) proc.
EDIT: I hadn't seen cdome's reply. That's probably what's happening. You need a mixin.
@cdome Mixin is not a concept I'm familiar with; I thought that was meant to be something basically like a "partial template". I've looked up mixin in the doc; from that example I still don't know what it does (looks like some kind of "symbol import"?), so I need a bit more direction. I tried adding:
mixin hash
mixin `==`
In the proc that creates the Table:
proc initSharedTable*[A, B](initialSize=64): SharedTable[A, B] =
assert isPowerOfTwo(initialSize)
mixin hash
mixin `==`
result.counter = 0
result.data = initSharedArray[KeyValuePair[A, B]](initialSize)
but it didn't seem to make any difference. Then I tried adding it directly at the top of the module, but the compiler says it's invalid there, then I tried adding it in the template where the error is ("hc = hash(key)"):
template genHashImpl(key, hc: typed) =
mixin hash
mixin `==`
hc = hash(key)
if hc == 0: # This almost never taken branch should be very predictable.
hc = 314159265 # Value doesn't matter; Any non-zero favorite is fine.
But the error doesn't go away. Where else should I put it?
@boia01 What I mean is, that I have a module that defines the type "SharedTable[A, B]", and in another module I have "SharedText", and in a third module (TypeRegister) I import both modules and define a type that contains a "SharedTable[SharedText, Whatever]", so I'm "passing SharedText as a generic parameter to SharedTable".
EDIT: I have (literally) not used git(hub) for years. Here is the repo with my code so far.
I got it to compile.
The problem you have (I think) is that the hash proc is not exported from typerekjister.nim, so it's not visible from kueues.nim and your tables implementation can't find it.
The changes I made:
typerekjist.nim:
export hash, `==`, id # export from tekst and rekjster
Output:
TESTING message queues ...
Message received after 0.0
Reply received after 0.0
compiling with nim c -r --threads:on kueues.nim on a Windows 10 x64, using gcc.