-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Definite assignment analysis and out parameters #378
Comments
Then why introduce such complexity for a feature that would only be used rarely anyways? IMO code like proc p(): seq[int] =
result.add 4 should keep working without warnings. proc p(b: bool): seq[int] =
if b:
result = @[4]
else:
echo "hmmm, did I forget to assign result here?" but not for the previous snippet. |
Can it produce a warning for I think that for 2.0 we need to reduce the amount of experimental switches, not duplicate it. |
I believe making default value assignment mandatory would break quite a lot of code, especially templates/macros and introduce unnecessary verbosity in the language. When I write
Seems like a good idea, but forcing initialization for type
CustomType = object
field: string
proc default(T: typedesc[CustomType]): T =
# Same is `init=` in my origina proposal
CustomType(field: "sfasdf")
# `var c: CustomType` is translated into
var c = default(CustomType)
echo c |
Whether or not this is accepted, I think a function that doesn't mention |
I agree with @haxscramper here :this seems very much like #252 Whether you call it |
This has been implemented is available under |
Proposal to add
out
parameters to Nim and to enable Nim's "definite assignment analysis"(https://docs.oracle.com/javase/specs/jls/se6/html/defAssign.html) for all types.
Current situation
This code compiles without warnings:
Variables (including
result
) are initialized to their default value automatically. Thisdesign has its merits but it is also considered to be error prone:
It's hard to know if the "implied" statement was meant and I think it's the most frequent
source of
nil
bugs. Nim's original design lackedsystem.default(T)
so initializing variablesto
default(T)
implicitly made some sense. Another benefit was this solution does notrequire an analysis over the control flow graph to ensure that no uninitialized values
can be accessed.
But for some types like
var T
orptr T not nil
there is nodefault
value -- variablesof such a type need to be initialized explicitly.
Proposal: Require explicit initialization
This RFC proposes to remove the special casing of variables that lack a default value
-- all variables need to be initialized explicitly before they can be used. This is what
C# and Java (see https://docs.oracle.com/javase/specs/jls/se6/html/defAssign.html) do
for decades and it works very well.
Proposal: Add out parameters to Nim
This proposal was implemented and found to not work too well with today's Nim. The
reason is that code like:
is very common. The
var File
here is an output parameter, not a read-writeparameter. This is why I propose to add
out T
parameters to Nim. While outputparameters are usually bad style, APIs from Windows and POSIX use output parameters
heavily.
When the implementation of a routine with output parameters is analysed, the compiler
checks that every path before the (implicit or explicit) return does set every output
parameter:
Out parameters and exception handling
This analysis takes exceptions into account which can produce annoying results:
However, I think this is acceptable for two reasons:
values instead.
via
outParam = default(typeof(outParam))
.Out parameters and inheritance
It is not valid to pass an lvalue of a supertype to an
out T
parameter:However, in the future this could be allowed and give us a better way to write object
constructors that take inheritance into account.
Code breakage / Transition period
Code like the following
var x: T; use x
is not allowed anymore. As a long transitionperiod the compiler will warn about the "usage before definition" and translate it as
if
var x = default(T); use x
was written.Future extensions
Object fields that are declared as
field: T
must be explicitly initialized in objectconstructors, to get a default value for an object field the syntax
field = default(T)
should be used.
Alternatives
out
parameters.The text was updated successfully, but these errors were encountered: