Skip to content

Interaction between generics and dirty templates #12283

@andreaferretti

Description

@andreaferretti

Easier to explain with an example

import sequtils

var b = 5

type Foo[T] = object
  h, w: int

proc bar[T](foos: seq[Foo[T]]) =
  let w = foldl(foos, a + b.w, 0)

  echo w

let foos = @[Foo[int](h: 3, w: 5), Foo[int](h: 4, w: 6)]
bar(foos)

Here I am using foldl, which injects the variables a and b. Apparently, this goes in conflict with the existing variable b. This only happens inside a generic procedure, as above.

The output is

/Users/andrea/playground/nim/example.nim(14, 4) template/generic instantiation of `bar` from here
/Users/andrea/playground/nim/example.nim(9, 28) Error: undeclared field: 'w' for type system.int [declared in /Users/andrea/.mynim/devel/lib/system.nim(26, 3)]

Currently using

Nim Compiler Version 1.0.99 [MacOSX: amd64]
Compiled at 2019-09-27
Copyright (c) 2006-2019 by Andreas Rumpf

git hash: ce4eb4ac916a9b1c7dae42bfb0626a28bbaf5fdf
active boot switches: -d:release

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions