mattaylor / elvis Goto Github PK
View Code? Open in Web Editor NEWtruthy, ternary, elvis and conditional assignment and conditional access operators for nim
License: MIT License
truthy, ternary, elvis and conditional assignment and conditional access operators for nim
License: MIT License
Lines 20 to 23 in 1ad14f1
We only need the last line with the typeclass. It's equivalent to the top three. https://nim-lang.github.io/Nim/manual.html#generics-type-classes
Current conditional access templates fail to compile when matching function chains with extra args
The following fail to compile...
let tab = { "one": "uno" }.toTable
check(tab.?getOrDefault("one") == "uno")
check(tab.?getOrDefault("two") == "")
check(tab.?getOrDefault("two", "unknown") == "unknown")
Thanks for the changes! I was going to push a PR, but you saved me the trouble. :)
I'm currently, trying to figure out how to chain .?
so we can do:
var val = obj.?data.?val
I'm looking at https://nim-lang.github.io/Nim/manual_experimental.html#special-operators-dot-operators or https://nim-lang.github.io/Nim/manual_experimental.html#pattern-operators-the-nimstar-operator but I haven't figured it out yet.
I'm doing interop with C, and I have lots of structures that have pointer values. I want to use a conditional access operator in combination with a truthy operator like this:
if ?data?.shader:
# use the data.shader
But the current implementation expects shader to be a proc. Should I create a new operator for my use case or do you think ?.
could be modified to handle this?
?.
has lower precedence than ==
https://nim-lang.github.io/Nim/manual.html#syntax-precedence
If we wanted to do something like:
if d?.val == 10:
echo "foo"
We would need to wrap the expression in parentheses:
if (d?.val) == 10:
echo "foo"
If we change the operator to .?
then this works:
if d.?val == 10:
echo "foo"
I've been working with a C library, and the try/catch mechanism doesn't work well for digging into structs.
For example, I call function that returns a ptr
.
let draw: ptr tm_creation_graph_draw_call_data_t = render_component_api.draw_call(...)
if ?draw.shader:
# do stuff
If draw
is nil then I'll get a crash because the access violation is coming from C and not from Nim. My work around is to use my conditional access macro that does checks at each step.
if ?draw.?shader:
# do stuff
Not quite sure what is going on, but I do have a minimal reproducible example:
import elvis
var a: seq[int] = @[]
echo ?(a[0])
type Story* = object
var stories: seq[Story]
echo ?(stories[0])
This prints
false
true
which is pretty counter intuitive, as I'd expect the second echo to print false as well.
https://play.nim-lang.org/#ix=3MaY
Line 19 in 09e536d
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.