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]

proc PresentModeB*[T:string|seq[byte]](d: T) =
    echo "B Presenting: ", $d[0]
#main app
    os, sequtils, strutils,
    lib1, lib2

    modes = enum
        typeA, tyepB
    mode = typeA

template PresentGeneric(s: typed) =
    var dispatcher = @[PresentModeA, PresentModeB]

proc main() =
    # 'mode' is really dependant on optargs
    let data = @[0x00.byte, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07]
    let text = "Something to shout about."


Is this design supported or should avoid generics pointers?

Thank you.

2018-03-10 23:12:43
Compiler bug aside, you need to instantiate the procs, like @[presentModeA[string], presentModeB[string]].
2018-03-11 12:38:34

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.

2018-03-11 21:13:46

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) = ...

2018-03-12 07:35:26