When the string parameter to readline is not initialized, the procedure segfaults. This occurs because it tries to reuse an existing string, but does not check if it has actually been initialized.
var f: TFile
discard open(f, "example.txt", fmRead)
var line: string
if not readLine(f, line):
The compiler can't really check if a variable has been initialized, because nimrod as a language does allow null pointers. What would you have the compiler do? Check for nil at runtime?
The compiler shouldn't check it. The readLine implementation should. E.g.:
if line == nil:
line = ""
setLen(line.string, 0) # reuse the buffer!
The docs for readLine state "line must not be nil!" which implies that this is intentional, i'm not sure why though. @Araq will know more.
@dom96: I suspect that it would be for performance reasons (though the gain is minimal, given that we're talking about a function likely dominated by other things, especially I/O, and branch prediction eliminating most of it).
The problem is that this is a somewhat fragile contract; it can be violated by accident in a form that testing does not uncover until the code is in production (e.g., if the line variable is initialized only along one code path).
As discussed with @Araq on IRC, this behavior is likely to change in the future, when strings will be initialized to an empty string instead of nil. Another planned feature is not nil annotations for parameters that the compiler will be able to enforce at compile-time for reference types in situations similar to this.
This is in no way a showstopper.
Blocked until not nil is ready.