I have stumbled this error: "nim c main" gives "SIGSEGV: Illegal storage access. (Attempt to read from nil?)" on machine setup: Nim Compiler Version 0.18.0 [Windows: amd64], "uname -a" -> MINGW64_NT-10.0 <name> 2.9.0(0.318/5/3) 2017-09-13 23:16 x86_64 Msys
Simple sources are as:
# lib1.nim
proc PresentModeA*[T:string|seq[byte]](d: T) =
echo "A Presenting: ", $d[0]
#lib2.nim
proc PresentModeB*[T:string|seq[byte]](d: T) =
echo "B Presenting: ", $d[0]
#main app
import
os, sequtils, strutils,
lib1, lib2
type
modes = enum
typeA, tyepB
var
mode = typeA
template PresentGeneric(s: typed) =
var dispatcher = @[PresentModeA, PresentModeB]
dispatcher[mode.ord](s)
proc main() =
# 'mode' is really dependant on optargs
let data = @[0x00.byte, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]
PresentGeneric(data)
let text = "Something to shout about."
PresentGeneric(text)
main()
Is this design supported or should avoid generics pointers?
Thank you.
Great, this works:
var dispatcher = @[PresentModeA[type(s)], PresentModeB[type(s)]]
what doesn't work though is if the implementations of PresentMode_A/B() are then expanded separately for other types:
proc PresentModeA*[T:string|seq[byte]](d: T) = ...
proc PresentModeA*(d: bool) = ...
the error in main being: Error: cannot instantiate: 'PresentModeA[type(some_bool_var)]'
But that would be stretching it I suppose. So I'm happy with the single implementation, as long as I remember the limitation.
Thanks again.
Probably because the overloaded PresentModeA is not generic so you can't use this syntax PresentModeA[Foo], try with that:
proc PresentModeA*[T: bool](d: T) = ...