Am I the only one who finds both proposed syntaxes somewhat confusing?
The originally proposed 'Bar[]' simply looks strange.
HHVM's introduces even more "special characters" (<, >, ?) to parse, looks
a bit like a regular expression and it was even already mentioned that it's
"imported" from C++ ... PHP is not C++, and why should it be?
So, how about this:
Array[Bar] $a - $a is array containing only instances of Bar
Array[Bar|null] $a - $a is array containing Bar instances or nulls
Array[Bar] $a = null - $a is either null or array containing only
instances of Bar
Array[Bar|null] $a = null - above, but the array can contain nulls
It's easily readable and everybody should be able to recognize what this
thing does even without prior knowledge.
Hell, why not Array[Foo|Bar|stdClass|whocares] ?
Or even (at a later stage):
Array[]["key1", "key2"]
... which would hint for a numeric index array containing associative
arrays that in turn must have the defined keys. This is indeed a bit crazy,
but possible to add in the future without BC breaks. You could even shove
scalar typehints in there as soon as they are implemented.
Cheers.
Andrey.
Am I the only one who finds both proposed syntaxes somewhat confusing?
The originally proposed 'Bar[]' simply looks strange.
It is, however, familiar to C, C++, C# and Java users.
So, how about this:
Array[Bar] $a - $a is array containing only instances of Bar
Array[Bar|null] $a - $a is array containing Bar instances or nulls
Array[Bar] $a = null - $a is either null or array containing only
instances of Bar
Array[Bar|null] $a = null - above, but the array can contain nulls
That doesn't make much sense as a syntax, IMO. It looks like "Bar" is
the index of the Array here. Surely it could use angle brackets, which
would be familiar to people who've dealt with generics (C++, C#, Java
users):
Array<Bar> $a
It's easily readable and everybody should be able to recognize what this
thing does even without prior knowledge.
It's not clear to me at all at first glance. Is that a Bar-indexed array?
Or even (at a later stage):
Array[]["key1", "key2"]
... which would hint for a numeric index array containing associative
arrays that in turn must have the defined keys. This is indeed a bit crazy,
but possible to add in the future without BC breaks. You could even shove
scalar typehints in there as soon as they are implemented.
...that's even less intuitive to me.
Andrea Faulds
http://ajf.me/
Okay, so <Bar> for contents and [Bar] for keys, although AFAIK you can't
use objects as array keys.
These were all random thoughts, I haven't though about it all in depth.
Still, I believe it's more intuitive than both previous proposals.
Am I the only one who finds both proposed syntaxes somewhat confusing?
The originally proposed 'Bar[]' simply looks strange.
It is, however, familiar to C, C++, C# and Java users.
So, how about this:
Array[Bar] $a - $a is array containing only instances of Bar
Array[Bar|null] $a - $a is array containing Bar instances or nulls
Array[Bar] $a = null - $a is either null or array containing only
instances of Bar
Array[Bar|null] $a = null - above, but the array can contain nullsThat doesn't make much sense as a syntax, IMO. It looks like "Bar" is the
index of the Array here. Surely it could use angle brackets, which would be
familiar to people who've dealt with generics (C++, C#, Java users):Array<Bar> $a
It's easily readable and everybody should be able to recognize what this
thing does even without prior knowledge.
It's not clear to me at all at first glance. Is that a Bar-indexed array?
Or even (at a later stage):
Array[]["key1", "key2"]
... which would hint for a numeric index array containing associative
arrays that in turn must have the defined keys. This is indeed a bit
crazy,
but possible to add in the future without BC breaks. You could even shove
scalar typehints in there as soon as they are implemented....that's even less intuitive to me.
Andrea Faulds
http://ajf.me/
One last comment in favor of the HHVM syntax:
We're written (and will very soon be releasing, I promise) a static
analysis tool which looks at your entire code base and determines
types for every local variable in use and tells you (without having to
run the program) where you have type issues. This means two things:
- No need to do runtime checks (Which impact performance)
- No need to get to 100% code-coverage to guarantee type safety.
And the type-checker can run in the background re-evaluating the files
as they're updated for instant notification.
If PHP were to follow HHVM's syntax, we'd get the type-checker as a
freebie. (If and when we took on return and scalar type hints as
well)
-Sara
Hi!
If PHP were to follow HHVM's syntax, we'd get the type-checker as a
freebie. (If and when we took on return and scalar type hints as
well)
I'm pretty sure if such tool were open source, it would not be hard to
patch it to follow any syntax, provided underlying sematics is the same.
It probably parses the code into some kind of internal representation
anyway, so by slightly changing that parser you will be able to
accommodate any syntax.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
What I find interesting is that the title of the RFC is "Array Of",
proposing a type hint for an "array of Foo". In my opinion the proposed
syntax however reads more as "Foos in an array"?
Foo[] => Foo in/as an array
array<Foo> => array of Foo
I personally like the idea of array<Foo> because it seems to leave more
room for improvements and additions over time (nesting array<array<Foo>>,
k/v typehints array<Foo, Bar>).
Best,
Alexander
Hi!
It's easily readable and everybody should be able to recognize what this
thing does even without prior knowledge.Hell, why not Array[Foo|Bar|stdClass|whocares] ?
Or even (at a later stage):
Array[]["key1", "key2"]
I don't think inventing another language to specify types inside PHP is
a good idea. If somebody is really into type calculus, there are many
academic languages to play with. In practice, such byzantine constructs
rarely make developer's work easier on tasks that PHP is commonly
applied to. If you want a language that can run code to check if your
array conforms to arbitrary set of rules, what's wrong with PHP language?
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227