It conflicts with the built-in find in system, how can I override it?
import sugar, options
proc find*[T](list: openarray[T], check: (T) -> bool): Option[T] =
for v in list:
if check(v): return v.some
T.none
echo @[1, 2, 3].find((v) => v == 2)
Try:
import system except find
before your other imports?There is a test for it in tests/modules/texplicit_system_import.nim. I haven't looked into the VC/github history as to how reliable it is.
If you really hate everything from system you can do this:
from system import nil
import options
proc find*[T](list: system.openarray[T],
check: (proc(v:T): system.bool)): Option[T] =
for v in system.items(list):
if check(v): return v.some
T.none
system.echo [1,2,3].find(proc(v: system.int): system.bool =
system.`==`(v,2))
Therein lies the "guidance" to not import system explicitly (or with the except or from system import nil constructs). Many facilities probably just assume namespace qualification is not necessary. In this example, the => macro from sugar wanted auto..So, I just de-sugared manually. There may be other ways to workaround it, though. In any event, "should never" is probably slightly too strong a wording for the guidance. "Don't unless you really need to/know what your doing/what the costs are" is probably a better way to phrase it.https://nim-lang.org/docs/system.html
Each module implicitly imports the System module; it must not be listed explicitly.
For me and google the translation seems to be "es darf nicht", that is it is forbidden.
Can we consider maybe renaming find to find_index and moving to sequtils?
And maybe limit its scope somehow, as currently it's impossible to override it and there are lots of methods that can use find name, like:
sequence.find((v) => v == 2)
db.collection("posts").find(id = 10)
...
btw, your example will work if you qualify parameter check
https://play.nim-lang.org/#ix=2yyj
import sugar, options proc find*[T](list: openarray[T], check: (T) -> bool): Option[T] = for v in list: if check(v): return v.some T.none echo @[1, 2, 3].find(check=((v) => v == 2))