2 ms·
I am curious about the safety, though. This minimal example causes a segfault: type Foo = ref object a: int var foo: Foo = nil echo foo.a
by etra0 4y ago
I am curious about the safety, though.
This minimal example causes a segfault:
type Foo = ref object
a: int
var foo: Foo = nil
echo foo.a
which wouldn't be simply possible to type in safe Rust.
- j-james 4y agoReference and pointer types (annotated with `ref` and `ptr` respectively) are nillable. The compiler can provide null safety guarantees by annotating types with `not nil`: https://nim-lang.github.io/Nim/manual_experimental.html#not-nil-annotation https://nim-lang.github.io/Nim/manual_experimental.html#not-.... So then your example would look like the following, and fail to compile. type Foo = ref object Bar = Foo not nil var bar: Bar = nil # error: type mismatch echo bar.a Discussion about not nil by default is here: https://github.com/nim-lang/Nim/issues/6638 https://github.com/nim-lang/Nim/issues/6638