jimn
10/17/2021, 8:11 PMif (!buf.hasRemaining() || read == -1) createWriteNode(buf.flip()) else null
with
infix operator fun <T> Boolean.rem(s: T) = { s }.takeIf { this }?.invoke()
...
(!buf.hasRemaining() || read == -1) % createWriteNode(buf.flip())
to guarantee that s
doesn't fire before comparison?
i actually prefer
inline val <reified T> T.`⟲` get() = { this }
inline infix operator fun <reified T> Boolean.rem(s: T) = s.`⟲`.takeIf { this }?.invoke()
as the style matches the idioms i use in in prior art but i just don't quite find the langauge lawyer parts i need to trust bytebuffer ops to this one, it has worked for more mundane ternary targets leading up to this but bytebuffers have effectsJoffrey
10/17/2021, 9:28 PMJoffrey
10/17/2021, 9:35 PMif (cond) value else null
?jimn
10/17/2021, 9:41 PMjimn
10/17/2021, 9:42 PMjimn
10/17/2021, 9:43 PMs%foo?:bar
if(s)foo else bar
[-----]
Joffrey
10/17/2021, 9:56 PMs % foo ?: bar
if(s) foo else bar
And using unconventional syntax with custom operators adds cognitive load to the reader as opposed to a simple standard if expression, which I personally don't find worth the saved 4 spaces (then it's a matter of opinion so there's not much to debate).
Maybe you could get away with the lazy execution problem thanks to inlining, as you did in the second version, but I would need to confirm that, as I'm not sure whether an intermediate variable for the function argument would be generated or not, and I'm afk right now so I can't test it.
If it doesn't save you from evaluating, you would have to wrap the passed value in braces when calling your operator, which would make it the same length as the if-else versionjimn
10/17/2021, 10:45 PM