Skip to content
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

Use "unit type" instead of "value type" #4332

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion language/types.xml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ int(16)
&language.types.void;
&language.types.never;
&language.types.relative-class-types;
&language.types.value;
&language.types.unit;
&language.types.iterable;
&language.types.declarations;
&language.types.type-juggling;
Expand Down
2 changes: 1 addition & 1 deletion language/types/declarations.xml
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ Stack trace:
<title>Union types</title>
<warning>
<simpara>
It is not possible to combine the two value types <type>false</type>
It is not possible to combine the two unit types <type>false</type>
and <type>true</type> together in a union type.
Use <type>bool</type> instead.
</simpara>
Expand Down
28 changes: 14 additions & 14 deletions language/types/type-system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
<listitem>
<simpara>Built-in types</simpara>
<itemizedlist>
<listitem>
<simpara><type>null</type> type</simpara>
</listitem>
<listitem>
<simpara>
Scalar types:
Expand Down Expand Up @@ -70,18 +67,21 @@
<type>self</type>, <type>parent</type>, and <type>static</type>
</simpara>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<simpara>
<link linkend="language.types.value">Value types</link>
</simpara>
<itemizedlist>
<listitem>
<simpara><type>false</type></simpara>
</listitem>
<listitem>
<simpara><type>true</type></simpara>
<simpara>
<link linkend="language.types.unit">Unit types</link>
</simpara>
<itemizedlist>
<listitem>
<simpara><type>false</type></simpara>
</listitem>
<listitem>
<simpara><type>true</type></simpara>
</listitem>
<listitem>
<simpara><type>null</type></simpara>
</listitem>
</itemizedlist>
</listitem>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree with this change, since gettype(false) returns boolean. Thus the boolean type is not simply the union of the true and the false type, but an independent type.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I made that claim anywhere in this PR. What exactly in this PR do you disagree with? Your comment points to the lines where I changed "Value type" to "Unit type", added a null entry and indented the list to be inside of "Built-in types". I don't think either one of these makes it seem as if boolean is a union of false and true.

Copy link
Member

@TimWolla TimWolla Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think either one of these makes it seem as if boolean is a union of false and true.

My reasoning goes like this:

  • Any given value can have only one type.
  • A unit type can contain only one value.
  • gettype(false) indicates that the value false is of type boolean.
  • That means it cannot be of type false.
  • That means that the type false has no members.
  • That means that the type false cannot actually exist as a unit type.

What exactly in this PR do you disagree with?

Effectively the entire PR, but here it becomes obvious that calling true and false a unit type does not reflect reality.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean, but that would imply that true and false cannot be used in type specifications as they are not types. And yet, we can write function foo(null $n, false $f, true $t). If we pass a wrong value as an argument we get:

Uncaught TypeError: foo(): Argument #2 ($f) must be of type false, true given

I don't agree with this axiom:

Any given value can have only one type.

Consider the following example:

<?php

class A {
    public function __invoke($x)
    {
        var_dump($x);
    }
}

$a = new A;
var_dump(gettype($a)); // object
var_dump(is_object($a)); // true
var_dump(is_a($a, A::class)); // true
var_dump(is_callable($a)); // true

This value belongs to at least 3 different types.

false and true are subtypes of the bool type.

If the current PR is unsuitable, I am open to suggestions how to document this better.

</itemizedlist>
</listitem>
Expand Down
17 changes: 9 additions & 8 deletions language/types/value.xml → language/types/unit.xml
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<sect1 xml:id="language.types.value">
<title>Value types</title>
<sect1 xml:id="language.types.unit">
<title>Unit types</title>

<para>
Value types are those which not only check the type of a value but also
the value itself. PHP has support for two value types:
<type>false</type> as of PHP 8.0.0, and <type>true</type>
as of PHP 8.2.0.
Unit types are those which allow only one value.
PHP has support for three unit types:
<type>false</type> as of PHP 8.0.0, <type>true</type>
as of PHP 8.2.0, and <type>null</type>.
</para>

<warning>
<simpara>
Prior to PHP 8.2.0 the <type>false</type> type could only be used as part of a
Prior to PHP 8.2.0 the <type>false</type> and <type>null</type> type
could only be used as part of a
<link linkend="language.types.type-system.composite.union">union type</link>.
</simpara>
</warning>

<note>
<simpara>
It is not possible to define custom value types. Consider using an
It is not possible to define custom unit types. Consider using an
<link linkend="language.types.enumerations">enumerations</link> instead.
</simpara>
</note>
Expand Down
Loading