Skip to content
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

truthy on nillable types does not produce the correct results #2

Closed
geekrelief opened this issue Jan 14, 2022 · 6 comments
Closed

truthy on nillable types does not produce the correct results #2

geekrelief opened this issue Jan 14, 2022 · 6 comments

Comments

@geekrelief
Copy link
Contributor

https://play.nim-lang.org/#ix=3MaY

template truthy*[T](val: T): bool = not compiles(val.isNil())

The implementation uses compile: https://nim-lang.github.io/Nim/system.html#compiles%2Cuntyped, which always returns true if val is a nillable type, but we want to actually check not val.isNil at runtime.

This will produce the expected results for nillable types.

# true if not isNil()
template truthy*[T](val: ref T): bool = not val.isNil
template truthy*[T](val: ptr T): bool = not val.isNil
template truthy*(val: pointer): bool = not val.isNil
@geekrelief geekrelief changed the title truthy on ref/ptr types does produce the correct results truthy on ref/ptr types does not produce the correct results Jan 14, 2022
@geekrelief geekrelief changed the title truthy on ref/ptr types does not produce the correct results truthy on nillable types does not produce the correct results Jan 14, 2022
@mattaylor
Copy link
Owner

fixed

@geekrelief
Copy link
Contributor Author

I think we still need the old version too otherwise the array test won't pass.

test "not empty seq": check(?[0])

That should say not empty array since a seq literal needs an @ prefix. Then again doing a truthy check on an array is kinda weird.

But if you put:

template truthy*[T](val: T): bool = not compiles(val.isNil())

Back then array[N, T] can match against it.

@mattaylor
Copy link
Owner

done - and updated tests

@geekrelief
Copy link
Contributor Author

cool, created a PR to clean up things based on a forum response from Hlaaftana. https://forum.nim-lang.org/t/8806#57402

@mattaylor
Copy link
Owner

good stuff - merged and ive added some extra tests and fixes for mutable chains like pop
thanks for your help!

@mattaylor
Copy link
Owner

I was trying to add condition access support for functions with extra args eg table.get(key), (see commented out tests) but Ive no idea how to capture the relevant args in the template to test. Do you know how this could be done, or do you think this would require a macro?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants