Skip to content

Instantly share code, notes, and snippets.

View Varriount's full-sized avatar

Clay Sweetser Varriount

View GitHub Profile
iterator sillyOne(x, y:int): int {.closure.} =
var z = x
while z < y:
yield z
inc(z)
when defined(isMainModule):
var x = sillyOne
echo(x(1, 20))
echo(x(1, 21))
iterator sillyOne(y:string): int {.closure.} =
var x = ""
while true:
x = x & y
yield x
when defined(isMainModule):
var x = sillyOne
echo(x("Hello"))
#-- 1
Traceback (most recent call last)
nimrod.nim(79) nimrod
nimrod.nim(55) HandleCmdLine
main.nim(308) MainCommand
main.nim(73) CommandCompileToC
modules.nim(193) CompileProject
modules.nim(151) compileModule
passes.nim(192) processModule
passes.nim(136) processTopLevelStmt
@Varriount
Varriount / generic.nim
Created November 7, 2013 18:37
Example of when a generic type for a procedure must be specified
import queues
when defined(isMainModule):
var x = initQueue[float](4)
add[float](x, 20) # Must be specified, else the compiler doesn't know if 20 is an int or a float
echo(x)
test.nim(38, 8) Info: instantiation from here
test.nim(28, 11) Info: instantiation from here
test.nim(38, 8) Info: instantiation from here
test.nim(28, 11) Info: instantiation from here
lib\pure\marshal.nim(242, 9) Error: internal error: instantiateGenericParamList: T
Traceback (most recent call last)
nimrod.nim(79) nimrod
nimrod.nim(55) HandleCmdLine
main.nim(308) MainCommand
main.nim(73) CommandCompileToC
of nkBracketExpr: # Line 1923 of semexpr.nim
checkMinSonsLen(n, 1)
var s = qualifiedLookup(c, n.sons[0], {checkUndeclared})
if not isNil(n.sons[0]) and n.sons[0].kind in {nkDotExpr}:
var dotSon = n.sons[0]
if not isNil(dotSon):
var lastSon = dotSon.sons[dotSon.sons.len-1]
if not isNil(lastSon) and lastSon.kind in {nkIdent}:
var g = qualifiedLookup(c, lastSon, {checkUndeclared}) # Errors in system.nim, line 2203
test.nim(41, 8) Info: instantiation from here
test.nim(29, 13) Info: instantiation from here
test.nim(41, 8) Info: instantiation from here
test.nim(29, 13) Info: instantiation from here
lib\pure\marshal.nim(242, 9) Error: internal error: instantiateGenericParamList: T
Traceback (most recent call last)
nimrod.nim(79) nimrod
nimrod.nim(55) HandleCmdLine
main.nim(308) MainCommand
main.nim(73) CommandCompileToC
## Imports
import marshal
import macros
## Types
type TLog* = ref object
path: string
file: TFile
# This works
iterator testGenProc[iT, rT](iter:iT): rT {.closure.} =
for i in iter:
yield i
var x = testGenProc[seq[int],int]
echo(x(@[1,2,3]))
echo(x(@[1,2,3]))
# This doesn't
@Varriount
Varriount / itertools.nim
Last active December 28, 2015 08:19
c:\64\nimrod\lib\system.nim(390, 13) Error: cannot instantiate: 'T'
iterator cycle* [iterType, returnType](iter:iterType): returnType =
## An iterator returning elements from the iterable and saving a
## copy of each. When the iterable is exhausted, return elements
## from the saved copy. Repeats indefinitely.
##
## Parameters:
## iter: The iterator to cycle through
var cont:seq[returnType]
cont = newSeq[returnType]()
for element in iter: