I'm working with derived enum types and I'm encountering errors "template/generic instantiation of map" warnings that I don't understand.
Here's a shortened version of my code:
import sequtils, tables
type
AmbigNucleotide = enum
T, A, G, C, U, W, S, M, K, R, Y, B, D, H, V, N
DNA = range[T..C]
RNA = range[A..U]
const
tblDNAtoRNA = {A: A, T: U, G: G, C: C}.toTable
proc transcribe(n: DNA): RNA =
RNA tblDNAtoRNA[n]
proc transcribe(ns: seq[DNA]): seq[RNA] =
map(ns, transcribe)
# I've also tried map[DNA,RNA](ns, transcribe) with the same results
let dna = @[DNA A, T, T, G, C, C, T, T]
let rna = transcribe(dna)
echo "DNA: ", dna
echo "Transcribed RNA: ", rna
The code above compiles and works but I get the following warnings (edited to highlight key lines):
example02.nim(17, 6) template/generic instantiation of `map` from here
sequtils.nim(311, 10) Warning: Cannot prove that 'result' is initialized. This will become a compile time error in the future. [ProveInit]
My key point of confusion is why this is a generic instantiation of map at all. It seems the types are well defined.
template/generic instantiation of FOO from here just means that the problem was inside a template or generic (the problem itself is not that line).
For the error in this specific case check the documentation of Table, it should be initialized, thats the reason for the warning.
What's your Nim version?
Your code compile as is for me on both Nim 0.20.2 and Nim 1.0.2.
$ nim --version
Nim Compiler Version 1.0.2 [MacOSX: amd64]
Compiled at 2019-10-22
Copyright (c) 2006-2019 by Andreas Rumpf
git hash: 193b3c66bbeffafaebff166d24b9866f1eaaac0e
active boot switches: -d:release
As I noted, it compiles (and gives the right output) but gives the warning I described above.
You get this warning because 0 (or the enum equivalent) is not a valid value for the type RNA and newSeq elements are initialized to 0. In the case of map the warning is wrong since the elements are properly initialized but the compiler couldn't follow the control flow.
Example:
proc test =
var x = newSeq[range[1..1]](5)
You can add --warning[ProveInit]:off to your nim.cfg or add {.push warning[ProveInit]: off.} to your file.
Unrelated tip: If a table is constant and the keys are contiguous, you can just use an array to avoid hashing/lookups.
const
tblDNAtoRNA = [T: RNA U, A: A, G: G, C: C]
proc transcribe(n: DNA): RNA =
tblDNAtoRNA[n]