-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Template findFirst fails to compile, proc with same code works fine #19576
Comments
Well there are two issues here, the generic import options
type
RegistryKey = object
key : string
val : string
var regKey = @[
RegistryKey(key:"ica.sflogin.debug", val:"true"),
]
template findFirst[T](s: seq[T], pred: proc(x: T): bool): Option[T] =
var res = none(typeof T) # if no items satisfy the predicate
for x in s:
if pred(x):
res = some(x)
break
res
proc getval(searchKey: string): Option[string] =
let found = regKey.findFirst(proc (rk: RegistryKey): bool = rk.key == searchKey)
if found.isNone: none(string)
else: some(found.get().val)
when isMainModule:
assert(getval("strange") == none(string))
assert(getval("ica.sflogin.debug") == some("true")) |
Great thanks for your quick help!!
Andi Kahnt
Am 02.03.2022 02:25 schrieb Jason Beetham:
Well there are two issues here, the generic none(T) and also the using result inside a template, templates do not have result, so the above code is not proper. Slight work around ahead.
import options
type
RegistryKey = object
key : string
val : string
var regKey = @[
RegistryKey(key:"ica.sflogin.debug", val:"true"),
]
template findFirst[T](s: seq[T], pred: proc(x: T): bool): Option[T] =
var res = none(typeof T) # if no items satisfy the predicate
for x in s:
if pred(x):
res = some(x)
break
res
proc getval(searchKey: string): Option[string] =
let found = regKey.findFirst(proc (rk: RegistryKey): bool = rk.key == searchKey)
if found.isNone: none(string)
else: some(found.get().val)
when isMainModule:
assert(getval("strange") == none(string))
assert(getval("ica.sflogin.debug") == some("true"))
--
Reply to this email directly, view it on GitHub [1], or unsubscribe [2].
Triage notifications on the go with GitHub Mobile for iOS [3] or Android [4].
You are receiving this because you authored the thread.Message ID: ***@***.***>
Links:
------
[1] #19576 (comment)
[2]
https://github.com/notifications/unsubscribe-auth/AALK7JWOFSUBHFM62C5SGF3U527PZANCNFSM5PVIDYKQ
[3]
https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675
[4]
https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub
|
metagn
added a commit
to metagn/Nim
that referenced
this issue
Aug 23, 2023
fixes nim-lang#13527, fixes nim-lang#17240, fixes nim-lang#6340, fixes nim-lang#20033, fixes nim-lang#19576, fixes nim-lang#19076
metagn
added a commit
to metagn/Nim
that referenced
this issue
Aug 25, 2023
fixes nim-lang#13527, fixes nim-lang#17240, fixes nim-lang#6340, fixes nim-lang#20033, fixes nim-lang#19576, fixes nim-lang#19076
narimiran
pushed a commit
that referenced
this issue
Dec 1, 2023
narimiran
pushed a commit
that referenced
this issue
Dec 1, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi
I wrote a template findFirst() to find the first element in a seq[T] which fulfills a predicate - minimal source attached.
Compilation of template code raises:
If template code is copied to a "proc" and all "T" replaced by "RegistryKey" (the type), it works well. You can simply check that by changing a const "failure" in the source.
I work with nim-1.6.4
Thanks for help!
Source:
nimbug.zip
The text was updated successfully, but these errors were encountered: