Re:Nim binary size from 160 KB to 150 Bytes
by phaylon on Monday May 04, 2015 @01:20PM
(#9487554)
Out of curiosity: How do you convert from a nilable `Foo` to a `Foo not nil`? As in, what do you do if you have a function maybe returning a `Foo` and want to pass its value to a function taking `Foo not nil`?
[ Parent
| on HN ]
You prove to the compiler that the nilable var is not-nil via if statement. Eg:
proc foobar(f:Foo not nil) =
discard
let f = Foo() # nilable ref
let b: Foo not nil = f # Error, can't prove 'f' is not nil
if f != nil:
let b: Foo not nil = f # can assign non-nil vars to 'f'
foobar(f) # or can pass 'f' directlyAh, good to know. So Nim does static analysis on conditionals where Rust would use an explicit pattern match to get at the contained value.
Might be a good example for http://nim-lang.org/manual.html#not-nil-annotation