The code below would fail, because result is not allocated.
Why not allocate it automatically? Compiler knows that there's assignment on result that's nil, and the code is local, shouldn't require complicated analysis and increase compile time.
type Unit = ref object
name: string
proc build_unit(name: string): Unit =
# result.new - why not call it automatically?
result.name = name
echo build_unit("Zeratul").repr
Output
Traceback (most recent call last)
/usercode/in.nim(8) in
/usercode/in.nim(6) build_unit
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
{.experimental: "strictNotNil".}
type
Foo = object
field: int
proc pr(): ref Foo =
result.field = 90
And I get a nice warning. :-)