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

Conversation

kamil-tekiela
Copy link
Member

Comment on lines 70 to 85
<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.

Copy link
Member

@Girgias Girgias left a comment

Choose a reason for hiding this comment

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

"unit type" is not the correct terminology for true and false.
Using "singleton type" would be better here, as {true} ⊆ {true, false}.

null is the unit type because the is no atomic type which contains {null} as a subset.

See https://langdev.stackexchange.com/questions/1539/what-are-the-differences-between-unit-and-singleton-types

@Girgias
Copy link
Member

Girgias commented Jan 14, 2025

To continue on this example, the following enumerated type:

enum Suit
{
    case Hearts;
    case Diamonds;
    case Clubs;
    case Spades;
}

has 4 singleton types:

  • Suit::Hearts
  • Suit::Diamonds
  • Suit::Clubs
  • Suit::Spades

And it is possible to create a new unit type:

enum Unit
{
    case Unit;
}

which is only inhabited by a single type, the singleton Unit::Unit type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

null is also a value type
3 participants