Hello, everybody!
After using nullsafe operator for a while, I have some further thoughts.
In most cases when we make type nullable, we would use nullsafe operator if
we don't know how to deal with it. This causes clogging main code with this
? signs. Possible solution is to make nullsafe type in the first place
rather than duplicate nullsafe operator everywhere.
The idea is pretty simple.
Instead of doing this:
class Foo
{
public ?Bar $bar = null;
}
class Bar
{
public int $baz;
}
fas($foo->bar?->baz);
tes($foo->bar?->baz);
We can remove duplication of question mark in all usage places by making
type nullsafe.
class Foo
{
public ??Bar $bar = null;
}
class Bar
{
public int $baz;
}
fas($foo->bar->baz);
tes($foo->bar->baz);
Regarding union types:
class Foo
{
public ?null|Bar $bar;
public Baz|Tres|?null $res;
}
In first case, ?null|Bar
is long form of ??Bar
.
In second one, Baz|Tres|?null
value of this type can be either Baz or
Tres or "safe null". Safe null is basically an pseudo-object, which for any
interaction (property acces or method call) returns same "safe null".
For union types, safe null should only be allowed in disjunction with
object.
class Foo
{
// Error
public ??int $int;
// allowed
public ??object $o;
// allowed, at least one object
public ?null|Bar|int|array $val;
}
Special attention requires nullsafe mixed value. It doesn't require two
question marks, but one, because mixed itself has an null inside.
class Bar
{
// this is nullsafe mixed value
public ?mixed $mixed;
}
$bar->mixed->property;
Safe null must behave almost like null. So that $safeNull === null is
true. If we would really like to tell apart safe null from null, some
helper function may be introduced.
About method calls, this should be allowed:
$resultOrSafeNull = $valueOrSafeNull->someMethod();
Using nullsafe operator on nullsafe value should be allowed:
$valueOrSafeNull?->method();
$valueOrSafeNull?->property;
Nullsafe types should be allowed as:
- parameter types
- return types
- property types
I would really like to hear your thoughts on this.
Hello, everybody!
After using nullsafe operator for a while, I have some further thoughts.
In most cases when we make type nullable, we would use nullsafe operator
if we don't know how to deal with it. This causes clogging main code with
this ? signs. Possible solution is to make nullsafe type in the first place
rather than duplicate nullsafe operator everywhere.The idea is pretty simple.
Instead of doing this:
class Foo { public ?Bar $bar = null; } class Bar { public int $baz; } fas($foo->bar?->baz); tes($foo->bar?->baz);
We can remove duplication of question mark in all usage places by making
type nullsafe.class Foo { public ??Bar $bar = null; } class Bar { public int $baz; } fas($foo->bar->baz); tes($foo->bar->baz);
Regarding union types:
class Foo { public ?null|Bar $bar; public Baz|Tres|?null $res; }
In first case,
?null|Bar
is long form of??Bar
.
In second one,Baz|Tres|?null
value of this type can be either Baz or
Tres or "safe null". Safe null is basically an pseudo-object, which for any
interaction (property acces or method call) returns same "safe null".For union types, safe null should only be allowed in disjunction with
object.class Foo { // Error public ??int $int; // allowed public ??object $o; // allowed, at least one object public ?null|Bar|int|array $val; }
Special attention requires nullsafe mixed value. It doesn't require two
question marks, but one, because mixed itself has an null inside.class Bar { // this is nullsafe mixed value public ?mixed $mixed; } $bar->mixed->property;
Safe null must behave almost like null. So that $safeNull === null is
true. If we would really like to tell apart safe null from null, some
helper function may be introduced.About method calls, this should be allowed:
$resultOrSafeNull = $valueOrSafeNull->someMethod();
Using nullsafe operator on nullsafe value should be allowed:
$valueOrSafeNull?->method(); $valueOrSafeNull?->property;
Nullsafe types should be allowed as:
- parameter types
- return types
- property types
Le 25/06/2021 à 08:58, Eugene Sidelnyk a écrit :
I would really like to hear your thoughts on this.
Hello,
As being a user of more than one language, in which at least 2 have the
same nullsafe operator (PHP and TypeScript), I'm used to this syntax,
it's clear about the intent at the cost of a single char.
It places the nullsafe intent where you actually use it, instead of
making it implicit on a whole composition chain, which would then create
nullsafe behavior to be "always on" in the whole composition chain and
very (very) hard to debug.
I think this would create too many side effects.
Moreover, your proposed syntax is very hard to read, in my opinion, and
will probably cause confusion.
Regards,
--
Pierre