nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.41k stars 1.47k forks source link

Overloading by a local proc affected by code in other blocks #2900

Open bluenote10 opened 9 years ago

bluenote10 commented 9 years ago

An issue I discovered while testing this PR. Apparently, the issue is unrelated to the PR, and I couldn't find an existing issue for that. The problem is that overloading by a local proc does work in this example:

import tables
from hashes import THash

block:
  proc hash(x: int): THash {.inline.} =
    echo "overloaded hash"
    result = x

  var t = initTable[int, int]()
  t[0] = 0 # will call the overloaded hash

But if I add another (supposedly unrelated) code block, I can no longer use the overloaded function:

import tables
from hashes import THash

block:
  var t = initTable[int,int]()
  t[0] = 42 # comment this line and it works again!

block:
  proc hash(x: int): THash {.inline.} =
    echo "overloaded hash"
    result = x

  var t = initTable[int, int]()
  t[0] = 0 # now, this no longer calls the overloaded hash!
dom96 commented 9 years ago

This looks very subtle so I will make it High Priority, feel free to change that @Araq.

Araq commented 9 years ago

This is not supported properly. Instead we cache the instantiation of 'Table[int, int]' to reduce code sizes. Not sure how to solve this.

metagn commented 6 years ago

Currently the overload doesn't load at all unless it's top level. Issue can be closed?