Hello internals,
Zeev's idea to bring peace to the galaxy seems like a good idea, but
impossible to implement in practice.
But it got me thinking about how one might introduce static typing
into a dynamically typed language w/out breaking BC.
And then I had this crazy idea:
//int
$i = 0;
//string
$c = 'c';
//float
$pi = 3.14;
If static typing in php was only an opt-in kind-of-thing, would this
work? Could the parser be built to identify 3 or 4 different keywords
in comments and give warnings or fatal errors for type conversions of
variables that have the type specified in the immediately preceding
comment?
Just a (crazy) idea.
---- Fwentish
śr., 4 wrz 2019, 05:52 użytkownik Fwentish Aelondes fwentish@gmail.com
napisał:
Hello internals,
Zeev's idea to bring peace to the galaxy seems like a good idea, but
impossible to implement in practice.But it got me thinking about how one might introduce static typing
into a dynamically typed language w/out breaking BC.And then I had this crazy idea:
//int
$i = 0;//string
$c = 'c';//float
$pi = 3.14;If static typing in php was only an opt-in kind-of-thing, would this
work? Could the parser be built to identify 3 or 4 different keywords
in comments and give warnings or fatal errors for type conversions of
variables that have the type specified in the immediately preceding
comment?Just a (crazy) idea.
---- Fwentish
IMO it's crazy idea and we should not change the way comments work
especially inline comments which even aren't kept in opcache.
I think better approach would be to put type in front of first variable
declaration like:
[type] $variable = $value;
BR,
Michał Brzuchalski
Hi,
Would this warrant a having mixed types? By making every variable of type
"mixed" when no type is defined, regardless of what value it receives or is
initialized with, this would make an opt-in system. This behavior would
cater developers who prefer only strict types, only dynamic types, and
those that want to type their code bit by bit without putting declares that
affect whole files.
$foo = 'foo';
// same as
mixed $oneHundred = 100;
// difference being:
// $foo is accepted when 'string' is typed in the signature
// $oneHundred is accepted when 'int' is typed in the signature
// no longer mixed, only accepts their defined type as value
// and is accepted only as their defined type in signatures
string $foo = 'foo';
int $oneHundred = 100;
// does imply we might need custom type definitions in the future to not
have to work with mixed
typedef Stringable = string | MyToStringInterface;
Stringable $foo = (bool) rand(0, 1) ? 'foo' : new MyStringableObject('foo');
Note that this behavior would require making some decisions whether or not
in the future this opt-in behavior should change when a default value is
given, such as with C# and type inference when declaring a variable, based
on its assigned value.
Regards,
Lynn van der Berg
On Wed, Sep 4, 2019 at 8:12 AM Michał Brzuchalski <
michal.brzuchalski@gmail.com> wrote:
śr., 4 wrz 2019, 05:52 użytkownik Fwentish Aelondes fwentish@gmail.com
napisał:Hello internals,
Zeev's idea to bring peace to the galaxy seems like a good idea, but
impossible to implement in practice.But it got me thinking about how one might introduce static typing
into a dynamically typed language w/out breaking BC.And then I had this crazy idea:
//int
$i = 0;//string
$c = 'c';//float
$pi = 3.14;If static typing in php was only an opt-in kind-of-thing, would this
work? Could the parser be built to identify 3 or 4 different keywords
in comments and give warnings or fatal errors for type conversions of
variables that have the type specified in the immediately preceding
comment?Just a (crazy) idea.
---- Fwentish
IMO it's crazy idea and we should not change the way comments work
especially inline comments which even aren't kept in opcache.I think better approach would be to put type in front of first variable
declaration like:[type] $variable = $value;
BR,
Michał Brzuchalski
In some other languages the mixed type is called "variant".
https://en.m.wikipedia.org/wiki/Variant_type
I mostly remember it from VisualBasic.
Lynn kjarli@gmail.com schrieb am Mi., 4. Sep. 2019, 09:22:
Hi,
Would this warrant a having mixed types? By making every variable of type
"mixed" when no type is defined, regardless of what value it receives or is
initialized with, this would make an opt-in system. This behavior would
cater developers who prefer only strict types, only dynamic types, and
those that want to type their code bit by bit without putting declares that
affect whole files.$foo = 'foo'; // same as mixed $oneHundred = 100; // difference being: // $foo is accepted when 'string' is typed in the signature // $oneHundred is accepted when 'int' is typed in the signature // no longer mixed, only accepts their defined type as value // and is accepted only as their defined type in signatures string $foo = 'foo'; int $oneHundred = 100; // does imply we might need custom type definitions in the future to not have to work with mixed typedef Stringable = string | MyToStringInterface; Stringable $foo = (bool) rand(0, 1) ? 'foo' : new MyStringableObject('foo');
Note that this behavior would require making some decisions whether or not
in the future this opt-in behavior should change when a default value is
given, such as with C# and type inference when declaring a variable, based
on its assigned value.Regards,
Lynn van der BergOn Wed, Sep 4, 2019 at 8:12 AM Michał Brzuchalski <
michal.brzuchalski@gmail.com> wrote:śr., 4 wrz 2019, 05:52 użytkownik Fwentish Aelondes fwentish@gmail.com
napisał:Hello internals,
Zeev's idea to bring peace to the galaxy seems like a good idea, but
impossible to implement in practice.But it got me thinking about how one might introduce static typing
into a dynamically typed language w/out breaking BC.And then I had this crazy idea:
//int
$i = 0;//string
$c = 'c';//float
$pi = 3.14;If static typing in php was only an opt-in kind-of-thing, would this
work? Could the parser be built to identify 3 or 4 different keywords
in comments and give warnings or fatal errors for type conversions of
variables that have the type specified in the immediately preceding
comment?Just a (crazy) idea.
---- Fwentish
IMO it's crazy idea and we should not change the way comments work
especially inline comments which even aren't kept in opcache.I think better approach would be to put type in front of first variable
declaration like:[type] $variable = $value;
BR,
Michał Brzuchalski
Andreas Hennings wrote:
In some other languages the mixed type is called "variant".
https://en.m.wikipedia.org/wiki/Variant_type
I mostly remember it from VisualBasic.
Union types are probably better than specifying a variant or mixed type. At present, parameters and properties with no type specified are mixed by default. Nikita has just opened discussion on a new union types proposal in another thread.
Fwentish Aelondes wrote:
Hello internals,
Zeev's idea to bring peace to the galaxy seems like a good idea, but
impossible to implement in practice.But it got me thinking about how one might introduce static typing
into a dynamically typed language w/out breaking BC.And then I had this crazy idea:
//int
$i = 0;//string
$c = 'c';//float
$pi = 3.14;If static typing in php was only an opt-in kind-of-thing, would this
work? Could the parser be built to identify 3 or 4 different keywords
in comments and give warnings or fatal errors for type conversions of
variables that have the type specified in the immediately preceding
comment?
Static analyzers (like PHPStan) already honor types specified in comments above variables.
/* @param int */
$i = 0;
Michał Brzuchalski wrote:
IMO it's crazy idea and we should not change the way comments work
especially inline comments which even aren't kept in opcache.I think better approach would be to put type in front of first variable
declaration like:[type] $variable = $value;
I think specifying the type in front of the variable is the best option, and we have precedence for this with typed properties in PHP 7.4.
Cheers,
Ben
Andreas Hennings wrote:
In some other languages the mixed type is called "variant".
https://en.m.wikipedia.org/wiki/Variant_type
I mostly remember it from VisualBasic.Union types are probably better than specifying a variant or mixed type. At present, parameters and properties with no type specified are mixed by default. Nikita has just opened discussion on a new union types proposal in another thread.
Nothing wrong about union types, but this is a different topic.
A "mixed" or "variant" type is simply giving a name to what we already
have. Like the invention of the number zero.
Currently afaik a variable with a type specifier is simply a
variant/mixed variable with type checking added on. Which means every
variable has to store the current value type.
In other languages, typed variables are implemented without the
dynamic type information, and only the variant/mixed/union types need
this additional overhead.
If we introduce this to PHP in the future at some point, this would
allow for some nice optimization.
Fwentish Aelondes wrote:
Hello internals,
Zeev's idea to bring peace to the galaxy seems like a good idea, but
impossible to implement in practice.But it got me thinking about how one might introduce static typing
into a dynamically typed language w/out breaking BC.And then I had this crazy idea:
//int
$i = 0;//string
$c = 'c';//float
$pi = 3.14;If static typing in php was only an opt-in kind-of-thing, would this
work? Could the parser be built to identify 3 or 4 different keywords
in comments and give warnings or fatal errors for type conversions of
variables that have the type specified in the immediately preceding
comment?Static analyzers (like PHPStan) already honor types specified in comments above variables.
/* @param int */
$i = 0;Michał Brzuchalski wrote:
IMO it's crazy idea and we should not change the way comments work
especially inline comments which even aren't kept in opcache.I think better approach would be to put type in front of first variable
declaration like:[type] $variable = $value;
I think specifying the type in front of the variable is the best option, and we have precedence for this with typed properties in PHP 7.4.
Cheers,
Ben
Hi,
Would this warrant a having mixed types? By making every variable of type
"mixed" when no type is defined, regardless of what value it receives or is
initialized with, this would make an opt-in system. This behavior would
cater developers who prefer only strict types, only dynamic types, and
those that want to type their code bit by bit without putting declares that
affect whole files.$foo = 'foo'; // same as mixed $oneHundred = 100; // difference being: // $foo is accepted when 'string' is typed in the signature // $oneHundred is accepted when 'int' is typed in the signature // no longer mixed, only accepts their defined type as value // and is accepted only as their defined type in signatures string $foo = 'foo'; int $oneHundred = 100;
Yes, an implicit "mixed" type would be the only way it could be done, just like object properties that are not explicitly typed are implicitly "mixed". Whether there is a "mixed" type for explicit declaration is an open question; if it were added, it would IMO make sense to add for properties and parameters/returns, as well, for consistency. Whether or not that's wise in general I don't know.
// does imply we might need custom type definitions in the future to not
have to work with mixed
typedef Stringable = string | MyToStringInterface;
Stringable $foo = (bool) rand(0, 1) ? 'foo' : new MyStringableObject('foo');
There's a long list of reasons we want that functionality even before we get to variable types. :-) IIRC, it falls into the popular category of "this would be really useful but is harder than it sounds for complex implementation reasons, and a few people feel it's too much ceremony for a loose language." There's a number of features stuck in that bucket.
Note that this behavior would require making some decisions whether or not
in the future this opt-in behavior should change when a default value is
given, such as with C# and type inference when declaring a variable, based
on its assigned value.
I could see arguments either way, but BC would probably dictate that types are not set implicitly. Otherwise, this currently valid code would break:
$things = 'foo';
...
$things = ['foo', 'bar'];
if (is_array($things)) { ... }
else { ... }
(Whether or not that's good practice is beside the point; there's ample code like that out there and breaking it when we don't have to is a no-no.)
--Larry Garfield
Note that this behavior would require making some decisions whether or not
in the future this opt-in behavior should change when a default value is
given, such as with C# and type inference when declaring a variable, based
on its assigned value.
I think this would too easily lead to mistakes.
E.g. if in a git commit, "$x = 5.6;" is replaced with "$x = 5;" or "$x
= '5.6';", then would a reviewer notice that this implicitly changes
the type?
Also, what if the initialization is inside an if branch? Later the
if/else gets reordered, or one of the conditional branches gets
removed, and the variable changes its type?
Hello,
this would be very possible constant with the actual without breaking BC
allow declare var = value : type; -> throws if assignment + type fails
the grammar context is exactly the same than a function return.
Best.
On Wed, Sep 4, 2019 at 10:18 AM Andreas Hennings andreas@dqxtech.net
wrote:
Note that this behavior would require making some decisions whether or
not
in the future this opt-in behavior should change when a default value is
given, such as with C# and type inference when declaring a variable,
based
on its assigned value.I think this would too easily lead to mistakes.
E.g. if in a git commit, "$x = 5.6;" is replaced with "$x = 5;" or "$x
= '5.6';", then would a reviewer notice that this implicitly changes
the type?
Also, what if the initialization is inside an if branch? Later the
if/else gets reordered, or one of the conditional branches gets
removed, and the variable changes its type?