-
Notifications
You must be signed in to change notification settings - Fork 760
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
base: master
Are you sure you want to change the base?
Conversation
713e49c
to
0704534
Compare
language/types/type-system.xml
Outdated
<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> |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 valuefalse
is of typeboolean
.- 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.
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
To continue on this example, the following enumerated type:
has 4 singleton types:
And it is possible to create a new unit type:
which is only inhabited by a single type, the singleton |
f72cd60
to
88c6400
Compare
88c6400
to
2c50802
Compare
Fixes #4077
https://en.wikipedia.org/wiki/Unit_type