On Nov 16, 2:31 am, Ian Lance Taylor <
i...@google.com> wrote:
Let's see.
Pointers would by default be non-nullable, "safe".
Using ! in front of type would make all pointers in that type (or just
itself if it's a pointer ie. !*) nullable. So a nullable version of
struct containing non-nullable pointers could be easily made, and I
geuss a nullable pointer to nullable version of a struct would be !*!.
Alternatively some keyword notation could be used, but I'm not sure
that would fit with current Go syntax very well?
To make nullable pointer or struct temporarily safe, the syntax could
be:
ifsafe var [, var...] then {
// vars are as if they were declared without !
}
else {
// some nil pointers found
}
Keyword "ifsafe" is just a suggestion, but at least it's short and
matches nicely with "else". Test should not be piggypaged to regular
if, since it would mess up short-circuit condition evaluation, and
since it has a side effect inside "then"-part of if.
Note: ifsafe could be used with floating point values to filter out
NaNs and infinities. Some other uses might be sensible too. I'm not
sure if Go strings can be nil in addition to empty, but if they can
be, this would nicely work with them too.
Nullable types could not be dereferenced. There might be some rare
cases where it would be nice to dereference nullable pointers without
"ifsafe", but I don't think they'd be worth the complexity, and would
partly defeat the whole purpose of not being able to dereference nil.
When initializing non-nullable items, all non-nullable fields would
have to be initialized. Either composite literal or a constructor
method could be given, which could also receive eg. array index if
it's being used with array allocation. Constructor would have to
assign all non-nullable fields before return. This would be identical
to any "variable might be used before initialization" check by
compiler, performed at constructor return, as assigning to non-
nullable field would guarantee it to be non-null. In array
initialization, constructor could receive the array (or slice?) and
index, but so that all other elements of array would be considered
nullable, or possibly with special restriction that they would write-
only. Purpose of this is to allow constructor to initialize other
members of same array to point to itself.
Constructor method would not be specially declared, any method could
be constructor if used as a constructor (or give compiler error if it
did not meet constructor requirements). Would argument types of given
method be valid way (in Go compilation context) to determine what kind
of constructor it is? I mean, would it take single struct, pointer to
struct, array and index, etc? Considering that there is no
overloading, this should be rather simple, I think?
If a struct containted pointers to it's own type, pointer to itself
could be used instead of nil value (combined with a flag or something
if pointing to itself would have other meanings as well).
No "default values" to point to should be provided by compiler. If
programmer needs them, he should declare them himself as needed.
There. I'm sure there are some holes in above suggestion, but is there
anything fundamentally broken?