Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


Reference 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-.... 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




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: