# Nullsafe Types

- Thread: https://externals.io/message/115129
- Original archive: https://news-web.php.net/php.internals/115129
- Started by Eugene Sidelnyk on 2021-06-24T19:14:37+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #115129 — Eugene Sidelnyk — 2021-06-24T19:14:37+00:00

URL: https://externals.io/message/115129#115129

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:

```php

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.

```php
class Foo
{
  public ??Bar $bar = null;
}

class Bar
{
  public int $baz;
}

fas($foo->bar->baz);
tes($foo->bar->baz);

```

Regarding union types:

```php
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.

```php
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.

```php
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:

```php
$resultOrSafeNull = $valueOrSafeNull->someMethod();
```

Using nullsafe operator on nullsafe value should be allowed:

```php
$valueOrSafeNull?->method();
$valueOrSafeNull?->property;
```

Nullsafe types should be allowed as:
- parameter types
- return types
- property types

---

## #115134 — Eugene Sidelnyk — 2021-06-25T06:58:17+00:00

URL: https://externals.io/message/115129#115134 · In reply to: #115129

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:
>
> ```php
>
> 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.
>
> ```php
> class Foo
> {
>   public ??Bar $bar = null;
> }
>
> class Bar
> {
>   public int $baz;
> }
>
> fas($foo->bar->baz);
> tes($foo->bar->baz);
>
> ```
>
> Regarding union types:
>
> ```php
> 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.
>
> ```php
> 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.
>
> ```php
> class Bar
> {
>   // this is nullsafe mixed value
>   public ?mixed $mixed;
> }
>
> $bar->mixed->property;
> ```
>
> > […]
> 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:
>
> ```php
> $resultOrSafeNull = $valueOrSafeNull->someMethod();
> ```
>
> Using nullsafe operator on nullsafe value should be allowed:
>
> ```php
> $valueOrSafeNull?->method();
> $valueOrSafeNull?->property;
> ```
>
> Nullsafe types should be allowed as:
> - parameter types
> - return types
> - property types

---

## #115135 — Pierre — 2021-06-25T07:17:30+00:00

URL: https://externals.io/message/115129#115135 · In reply to: #115134

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
