Hey Everyone,
With the syntax improvements for 5.4 and some of the de-referencing work,
code looks so much sleeker then before, but there is still one use case
that really bugs me.
Given this code:
$width = (isset($config['width']))? $config['width'] : 300;
The code is pretty straight forward, if width is set in the array, use it
otherwise use 300.
This code would look much better if it could make use of the ternary
operator
$width = $config['width'] ?: 300;
The only reason for this to not work is: it throws a notice if the array
key is not there (which is the case we are covering anyway)
This is basically because the ternary operator does not do a internal
implicit isset, only an empty.
Does this seem like a possible improvement we can work on? Anyone
interested in championing the change?
--
Rafael Dohms
PHP Evangelist and Community Leader
http://www.rafaeldohms.com.br
http://www.phpsp.org.br
Rafael,
One point of clarity:
On Wed, Jul 18, 2012 at 10:15 AM, Rafael Dohms listas@rafaeldohms.com.brwrote:
Hey Everyone,
With the syntax improvements for 5.4 and some of the de-referencing work,
code looks so much sleeker then before, but there is still one use case
that really bugs me.Given this code:
$width = (isset($config['width']))? $config['width'] : 300;
The code is pretty straight forward, if width is set in the array, use it
otherwise use 300.This code would look much better if it could make use of the ternary
operator$width = $config['width'] ?: 300;
The only reason for this to not work is: it throws a notice if the array
key is not there (which is the case we are covering anyway)This is basically because the ternary operator does not do a internal
implicit isset, only an empty.
It does not do an empty. Empty would avoid notices as well. All it does is
an implicit bool cast...
Does this seem like a possible improvement we can work on? Anyone
interested in championing the change?
I like it in principle. My only concern is why wasn't it done this way in
the first place... Is there a reason?
Anthony
On Wed, Jul 18, 2012 at 4:20 PM, Anthony Ferrara ircmaxell@gmail.comwrote:
Does this seem like a possible improvement we can work on? Anyone
interested in championing the change?
I like it in principle. My only concern is why wasn't it done this way
in the first place... Is there a reason?
I don't recall correctly, there was a thread on this by David Coalier some
time ago, but it went completely off course and i remember BC breaks were
part of the reason. I think we have a good plan for BC breaks now, so why
not rethink this.
Anthony
--
Rafael Dohms
PHP Evangelist and Community Leader
http://www.rafaeldohms.com.br
http://www.phpsp.org.br
Sounds good. CoffeeScript (a lightweight language with JS semantics that
compiles to JS) has a existential operator, "?", and this looks similar and
would be very nice. It's not the same thing, of course. It also reminds me
of JavaScript's || behaviour.
On Wed, Jul 18, 2012 at 4:20 PM, Anthony Ferrara <ircmaxell@gmail.com
wrote:
Does this seem like a possible improvement we can work on? Anyone
interested in championing the change?
I like it in principle. My only concern is why wasn't it done this way
in the first place... Is there a reason?I don't recall correctly, there was a thread on this by David Coalier some
time ago, but it went completely off course and i remember BC breaks were
part of the reason. I think we have a good plan for BC breaks now, so why
not rethink this.Anthony
--
Rafael Dohms
PHP Evangelist and Community Leader
http://www.rafaeldohms.com.br
http://www.phpsp.org.br
On Wed, Jul 18, 2012 at 7:20 AM, Anthony Ferrara ircmaxell@gmail.comwrote:
On Wed, Jul 18, 2012 at 10:15 AM, Rafael Dohms <listas@rafaeldohms.com.br
wrote:
[...]
This is basically because the ternary operator does not do a internal
implicit isset, only an empty.It does not do an empty. Empty would avoid notices as well. All it does is
an implicit bool cast...Does this seem like a possible improvement we can work on? Anyone
interested in championing the change?I like it in principle. My only concern is why wasn't it done this way in
the first place... Is there a reason?
It changes the semantics. If the variable is set to a falsey value and ?:
uses an implicit isset, the value of the expression will be the falsey
value.
$config['width'] = ''
$width = $config['width'] ?: 300
# $width == ''
If !empty were used instead of isset, you could preserve semantics ($a ?:
dflt = !empty($a) ? $a : dflt).
It changes the semantics. If the variable is set to a falsey value and ?:
uses an implicit isset, the value of the expression will be the falsey
value.$config['width'] = ''
$width = $config['width'] ?: 300$width == ''
If !empty were used instead of isset, you could preserve semantics ($a ?:
dflt = !empty($a) ? $a : dflt).
Since this has been discussed before, here's a previous solution with zero
of those problems:
// A new operator.
$width = $config['width'] ?? 300;
// Actual behavior of the operator in pseudo code:
$width = isset($config['width']) ? $config['width'] : 300;
Why wasn't it implemented? No reason, it just wasn't. But people keep asking
about it, so it's only a matter of time.
Stan
On Wed, Jul 18, 2012 at 12:21 PM, Galen Wright-Watson ww.galen@gmail.comwrote:
On Wed, Jul 18, 2012 at 7:20 AM, Anthony Ferrara ircmaxell@gmail.comwrote:
On Wed, Jul 18, 2012 at 10:15 AM, Rafael Dohms <listas@rafaeldohms.com.br
wrote:
[...]
This is basically because the ternary operator does not do a internal
implicit isset, only an empty.It does not do an empty. Empty would avoid notices as well. All it does is
an implicit bool cast...Does this seem like a possible improvement we can work on? Anyone
interested in championing the change?I like it in principle. My only concern is why wasn't it done this way
in
the first place... Is there a reason?It changes the semantics. If the variable is set to a falsey value and ?:
uses an implicit isset, the value of the expression will be the falsey
value.$config['width'] = '' $width = $config['width'] ?: 300 # $width == ''
If !empty were used instead of isset, you could preserve semantics ($a ?:
dflt = !empty($a) ? $a : dflt).
However, this would break if someone were to use an explicit isset, so ?:
could only use an implicit !empty if the expression isn't an isset
expression (which starts to get messy, grammar-wise).
Looking over the previous discussion, people objected to altering the
behavior of ?: precisely because it currently generates notices, which they
desire.
The following use-cases were put forward as commonly occurring and verbose
enough to desire syntactic sugar:
- isset($arr['key']) ? $arr['key'] : dflt
- ! empty($arr['key']) ? $arr['key'] : dflt
- ! is_null($arr['key']) ? $arr['key'] : dflt (used instead of isset
version; both wouldn't appear in same code base) - $arr['key'] = isset($arr['key']) ? $arr['key'] : dflt
- isset($arr['key']) ? strtoupper($arr['key']) : dflt
The following new operators were suggested:
- new ternary based on isset (e.g. "$a ?? strtoupper($a) : 'DFLT'" with
implicit isset) - new shortcut ternary based on isset
- new shortcut ternary based on !empty (objection: not as useful as
isset, since !empty is equivalent to !! but with notice suppression) - new shortcut ternary based on !== null or ! is_null
- new shortcut ternary that only works on arrays and is based
on array_key_exists - indexing operator that suppress undefined index notices
- variable access that suppresses undefined variable notices
- assign-if-not-set operator (like ||= in Javascript)
Some objections to the above:
-
(new ternary) ?
-
(isset) Some developers always set variables and never want undefined
notice suppression (the "never-unset" style), which isset will do. However,
this isn't as common a style. -
(!empty) Not as useful as isset, since !empty is equivalent to !! but
with notice suppression. Also, !empty($a)?$a:dflt isn't used as much as
isset($a)?$a:dflt. -
(!== null / ! is_null) Not as useful as isset, since it doesn't
suppress notices, so isn't a replacement for the isset($a)?$a:dflt pattern.
However, it can be used in combination with 6 for the isset-based ternary
for arrays (e.g. "$arr?['key'] ?: 'dflt'"). -
(array_key_exists) Conceptually, anything unset has a null value, and
should thus be equivalent in value to something set to NULL.
array_key_exists distinguishes between unset and set to NULL, which
won't always match how some want to use the shortcut ternary. Thus, not as
useful as is_null. -
(indexing w/o notices) ?
-
(variable access) Probably do more harm than good. It's easy to
ensure a variable is set. -
(assign-if-not-set) For arrays, there's the alternative:
$arr += Array('key' => 'value');
This can also be used for a "never-unset" style to set defaults for an
array, which goes with the "! is_null($a) ? $a : dflt" pattern.
Various syntax proposals:
- "$a ?? $b : dflt" as a ternary, with shortcutting behavior.
- "$a ?? dflt" as a shortcut ternary
- "$a ?! dflt" as a shortcut ternary based on !empty (could be added
along with "??"; objection is that it could ambiguate PHP's syntax,
conflicting with the not operator) - "$a $: dflt" as a shortcut ternary
- "$arr?['key']" to suppress undefined index notices
- "$arr@['key']" to suppress undefined index notices
- "$?var" to suppress undefined variable notices
- "$a ??= dflt" as an assign-if-not-set operator (goes along with "??")
- "$a !?= dflt" as an assign-if-not-set operator ("!" suggests not-set)
- "$a $:= dflt" as an assign-if-not-set operator (goes along with "$:")
?? / isset vs ?? / is_null
Of the various proposals, "??" (with semantics of "isset($a) ? $a : dflt")
already has a precedence: in C# as the null-coalescing operator. While PHP
doesn't have non-nullable types (hence "??" isn't a necessity), using ?? is
perhaps the least surprising option, and (including "??=") would cover
use-cases 1 and 4. On the other hand, PHP's "??" wouldn't be the same as
C#, since C# requires that variables be declared. Arguably, having "??"
equivalent to "!is_null($a) ? $a : dflt" is closer to C# semantics. Also,
use-cases 2 and 3 would be left out, whereas operator proposals 4 (?? /
is_null), 6 (?[]) and 8 (??= / is_null) together cover use-cases 1, 3 and
- Back on the first hand, use-cases 2 and 3 are less common and this is
(after all) merely syntactic sugar, which should be targeting the common
cases.
??: vs ??
The ternary "??:" (whether based on isset or is_null) can also cover
use-case 5, but isn't so good for case 4 ("??:=" would be ugly). One one
hand, the short-cut "??:" may be surprising to developers familiar with
C#'s "??:". On the other, PHP ain't C#, and documentation can cover "??:".
Besides, "??" isn't the only null-coalescing operator out there, so why
should C# be given special consideration? Less rhetorically, would
supporting both "??" and "??:", with "$a ??: dflt" equivalent to "$a ?? dflt",
be undesirable?
Personally, I support syntaxes 8. ??= with either 1. ??: / isset or
(1. ??:/ is_null and 5.
?[]).
Top-posting, as not sure where to snip. :)
I think either "??" "??:" as an operator makes sense, and would be
readable and easy to learn, and resolve any BC concerns people have. I'd
love to see this in, as the current behavior is basically useless for me
I don't think I've once had code where I could successfully use it
without worrying about the E_NOTICE, which then forces me to use the
longhand form. Having a version that does an implicit "isset" would be
tremendously useful.
--0016e6d64661f7e32504c521836f
Content-Type: text/plain; charset=ISO-8859-1On Wed, Jul 18, 2012 at 12:21 PM, Galen Wright-Watson ww.galen@gmail.comwrote:
On Wed, Jul 18, 2012 at 7:20 AM, Anthony Ferrara ircmaxell@gmail.comwrote:
On Wed, Jul 18, 2012 at 10:15 AM, Rafael Dohms <listas@rafaeldohms.com.br
wrote:
[...]
This is basically because the ternary operator does not do a internal
implicit isset, only an empty.It does not do an empty. Empty would avoid notices as well. All it does is
an implicit bool cast...Does this seem like a possible improvement we can work on? Anyone
interested in championing the change?I like it in principle. My only concern is why wasn't it done this way
in
the first place... Is there a reason?It changes the semantics. If the variable is set to a falsey value and ?:
uses an implicit isset, the value of the expression will be the falsey
value.$config['width'] = '' $width = $config['width'] ?: 300 # $width == ''
If !empty were used instead of isset, you could preserve semantics ($a ?:
dflt = !empty($a) ? $a : dflt).However, this would break if someone were to use an explicit isset, so ?:
could only use an implicit !empty if the expression isn't an isset
expression (which starts to get messy, grammar-wise).Looking over the previous discussion, people objected to altering the
behavior of ?: precisely because it currently generates notices, which they
desire.The following use-cases were put forward as commonly occurring and verbose
enough to desire syntactic sugar:
- isset($arr['key']) ? $arr['key'] : dflt
- ! empty($arr['key']) ? $arr['key'] : dflt
- ! is_null($arr['key']) ? $arr['key'] : dflt (used instead of isset
version; both wouldn't appear in same code base)- $arr['key'] = isset($arr['key']) ? $arr['key'] : dflt
- isset($arr['key']) ? strtoupper($arr['key']) : dflt
The following new operators were suggested:
- new ternary based on isset (e.g. "$a ?? strtoupper($a) : 'DFLT'" with
implicit isset)- new shortcut ternary based on isset
- new shortcut ternary based on !empty (objection: not as useful as
isset, since !empty is equivalent to !! but with notice suppression)- new shortcut ternary based on !== null or ! is_null
- new shortcut ternary that only works on arrays and is based
on array_key_exists- indexing operator that suppress undefined index notices
- variable access that suppresses undefined variable notices
- assign-if-not-set operator (like ||= in Javascript)
Some objections to the above:
(new ternary) ?
(isset) Some developers always set variables and never want undefined
notice suppression (the "never-unset" style), which isset will do. However,
this isn't as common a style.(!empty) Not as useful as isset, since !empty is equivalent to !! but
with notice suppression. Also, !empty($a)?$a:dflt isn't used as much as
isset($a)?$a:dflt.(!== null / ! is_null) Not as useful as isset, since it doesn't
suppress notices, so isn't a replacement for the isset($a)?$a:dflt pattern.
However, it can be used in combination with 6 for the isset-based ternary
for arrays (e.g. "$arr?['key'] ?: 'dflt'").(array_key_exists) Conceptually, anything unset has a null value, and
should thus be equivalent in value to something set to NULL.
array_key_exists distinguishes between unset and set to NULL, which
won't always match how some want to use the shortcut ternary. Thus, not as
useful as is_null.(indexing w/o notices) ?
(variable access) Probably do more harm than good. It's easy to
ensure a variable is set.(assign-if-not-set) For arrays, there's the alternative:
$arr += Array('key' => 'value');
This can also be used for a "never-unset" style to set defaults for an
array, which goes with the "! is_null($a) ? $a : dflt" pattern.Various syntax proposals:
- "$a ?? $b : dflt" as a ternary, with shortcutting behavior.
- "$a ?? dflt" as a shortcut ternary
- "$a ?! dflt" as a shortcut ternary based on !empty (could be added
along with "??"; objection is that it could ambiguate PHP's syntax,
conflicting with the not operator)- "$a $: dflt" as a shortcut ternary
- "$arr?['key']" to suppress undefined index notices
- "$arr@['key']" to suppress undefined index notices
- "$?var" to suppress undefined variable notices
- "$a ??= dflt" as an assign-if-not-set operator (goes along with "??")
- "$a !?= dflt" as an assign-if-not-set operator ("!" suggests not-set)
- "$a $:= dflt" as an assign-if-not-set operator (goes along with "$:")
?? / isset vs ?? / is_null
Of the various proposals, "??" (with semantics of "isset($a) ? $a : dflt")
already has a precedence: in C# as the null-coalescing operator. While PHP
doesn't have non-nullable types (hence "??" isn't a necessity), using ?? is
perhaps the least surprising option, and (including "??=") would cover
use-cases 1 and 4. On the other hand, PHP's "??" wouldn't be the same as
C#, since C# requires that variables be declared. Arguably, having "??"
equivalent to "!is_null($a) ? $a : dflt" is closer to C# semantics. Also,
use-cases 2 and 3 would be left out, whereas operator proposals 4 (?? /
is_null), 6 (?[]) and 8 (??= / is_null) together cover use-cases 1, 3 and
- Back on the first hand, use-cases 2 and 3 are less common and this is
(after all) merely syntactic sugar, which should be targeting the common
cases.??: vs ??
The ternary "??:" (whether based on isset or is_null) can also cover
use-case 5, but isn't so good for case 4 ("??:=" would be ugly). One one
hand, the short-cut "??:" may be surprising to developers familiar with
C#'s "??:". On the other, PHP ain't C#, and documentation can cover "??:".
Besides, "??" isn't the only null-coalescing operator out there, so why
should C# be given special consideration? Less rhetorically, would
supporting both "??" and "??:", with "$a ??: dflt" equivalent to "$a ?? dflt",
be undesirable?Personally, I support syntaxes 8. ??= with either 1. ??: / isset or
(1. ??:/ is_null and 5.
?[]).--0016e6d64661f7e32504c521836f--
--
Matthew Weier O'Phinney
Project Lead | matthew@zend.com
Zend Framework | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc
Hi!
Top-posting, as not sure where to snip. :)
I think either "??" "??:" as an operator makes sense, and would be
readable and easy to learn, and resolve any BC concerns people have. I'd
love to see this in, as the current behavior is basically useless for me
I don't think I've once had code where I could successfully use it
without worrying about the E_NOTICE, which then forces me to use the
longhand form. Having a version that does an implicit "isset" would be
tremendously useful.
I think we've painted ourselves into a corner somewhat here, because we
have the following things clashing:
- Producing an error when accessing non-existing var/element.
- Unability to temporarily disable 1 since @ both is regarded as
"unclean" and does not actually disable the error generation, only makes
it not printed out. - Obvious need to express common pattern "take it if it exists, assume
null if it does not".
Note that the case in 3 is not limited to ?: in any way, you as
frequently encounter it in any context where you can encounter a
variable. We can get back to ?: if we extend 3 with "take it if it
exists, assume this other value if it does not" - but we don't have to,
as the use case is completely dominated by "null" pattern, it is very
rare that you need any other default than null and type conversions of
null like ''/false/0.
I'd be very happy if we could have something like @$var['foo'] just work
by not generating any errors, but if not then probably having some
syntax saying "this variable or null, no notices" will be good - if say
it's $?foo then we could easily add defaults by doing $?foo ?: 'bar'.
Making @$var magically work though would have an advantage of being
instantly backwards compatible.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Hi,
I'd be very happy if we could have something like @$var['foo'] just work
by not generating any errors, but if not then probably having some
syntax saying "this variable or null, no notices" will be good - if say
it's $?foo then we could easily add defaults by doing $?foo ?: 'bar'.
Making @$var magically work though would have an advantage of being
instantly backwards compatible.
I'm lately using @$var["key"] a lot (despite it's slowness) and I like it a lot. It makes
most code a lot more readable. I would be +1 for it, if we can make it fast.
I think most people (me included) want only to avoid the notice for accessing undefined array offsets. What if we just remove the notice for accessing undefined array offsets, because it's the most common use case?
I've looked a bit into other languages (Ruby, Python, Go) and
all of them do not generate an error when an undefined dict/hash/map key is accessed.
Christoph Hochstrasser
http://twitter.com/hochchristoph | http://christophh.net | https://github.com/CHH
Am Samstag, 21. Juli 2012 um 01:10 schrieb Stas Malyshev:
Hi!
Top-posting, as not sure where to snip. :)
I think either "??" "??:" as an operator makes sense, and would be
readable and easy to learn, and resolve any BC concerns people have. I'd
love to see this in, as the current behavior is basically useless for me
I don't think I've once had code where I could successfully use it
without worrying about the E_NOTICE, which then forces me to use the
longhand form. Having a version that does an implicit "isset" would be
tremendously useful.I think we've painted ourselves into a corner somewhat here, because we
have the following things clashing:
- Producing an error when accessing non-existing var/element.
- Unability to temporarily disable 1 since @ both is regarded as
"unclean" and does not actually disable the error generation, only makes
it not printed out.- Obvious need to express common pattern "take it if it exists, assume
null if it does not".Note that the case in 3 is not limited to ?: in any way, you as
frequently encounter it in any context where you can encounter a
variable. We can get back to ?: if we extend 3 with "take it if it
exists, assume this other value if it does not" - but we don't have to,
as the use case is completely dominated by "null" pattern, it is very
rare that you need any other default than null and type conversions of
null like ''/false/0.--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Hi!
I've looked a bit into other languages (Ruby, Python, Go) and
all of them do not generate an error when an undefined dict/hash/map key is accessed.
Python would definitely throw an exception:
a = {}
a
{}
print a['a']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'a'
And it's just as annoying as in PHP. Actually, probably more annoying
:). But it has .get() which solves the problem usually, albeit in a bit
more verbose way. Also, Python has defaultdict which I would usually use
when I need keys to be created automatically.
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Hi,
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'a'And it's just as annoying as in PHP. Actually, probably more annoying
:). But it has .get() which solves the problem usually, albeit in a bit
more verbose way. Also, Python has defaultdict which I would usually use
when I need keys to be created automatically.
Ah, ok sorry about that…
This means then, that Go is less strict about this than Python.
Consider this:
http://play.golang.org/p/Qbc0bEBsKa
Christoph Hochstrasser
http://twitter.com/hochchristoph | http://christophh.net | https://github.com/CHH
Am Mittwoch, 01. August 2012 um 19:54 schrieb Stas Malyshev:
Hi!
I've looked a bit into other languages (Ruby, Python, Go) and
all of them do not generate an error when an undefined dict/hash/map key is accessed.Python would definitely throw an exception:
a = {}
a{}
print a['a']
--
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
2012/8/1 Christoph Hochstrasser christoph.hochstrasser@gmail.com:
I'm lately using @$var["key"] a lot (despite it's slowness) and I like it a lot. It makes
most code a lot more readable. I would be +1 for it, if we can make it fast.I think most people (me included) want only to avoid the notice for accessing undefined array offsets. What if we just remove the notice for accessing undefined array offsets, because it's the most common use case?
Getting philosophical. It's the same as if you want to read a page in
a book, but that page doesn't exist. For example a book with 40 pages,
but you want to read page 42. :) Is it an error to try it? Yes,
because if possible without error, you could do the same with the
universe and look at a part of it that doesn't exist. Someone has to
create it first. :)
More serious: For me it's the other way arround, it's one of the best
things, that have been introduced, because before it, a very small
write error had big impacts.
I see - as you - the need to make simple things more short, but
removing warnings from existing code constructs is definitly the wrong
way.
--
Alex Aulbach
So... I was thinking of proposing that we perhaps leave Arrays as is
w/r/t undefined indices, while fixing up the ArrayObject gaps and
making that the "smart" one (wrap/box in an AO if you want
expanded/overloadable functionality).
That idea was based on my belief that ArrayObject::offsetGet already
failed gracefully, from the docs: "Returns: The value at the specified
index or FALSE." Don't think so. Seems like it returns NULL
and fires
a notice as with an array.
Doc bug I guess, as as returning FALSE
would be pretty ambiguous.
-- S.
With the syntax improvements for 5.4 and some of the de-referencing
work,
code looks so much sleeker then before, but there is still one use
case
that really bugs me.Given this code:
$width = (isset($config['width']))? $config['width'] : 300;
Do we have to do this again?... We had this discussion an year ago.
See
http://grokbase.com/t/php/php-internals/113zbx5y77/implicit-isset-isempty-check-on-short-ternary-operator
http://grokbase.com/t/php/php-internals/114br1w6gs/proposed-access-modifier-silent-was-re-php-dev-implicit-isset-isempty-check-on-short-ternary-operator
--
Gustavo Lopes
Do we really need that as operator? Why not using new functions for
special cases.
Don't see much difference between
$a = $b ?: $c;
and (for example I used "i" for "if")
$a = _i($b, $c);
Just a manner of getting accustomed to it.
A little bit more thinking: This could be implemented directly into
any program/project, so it is a manner of how to use PHP and not of
why it isn't added to the language.
PS: Would it be possible to implement functions like ":?()" or "!?()"
? Currently this is not allowed by syntax.
2012/7/18 Rafael Dohms listas@rafaeldohms.com.br:
$width = $config['width'] ?: 300;
The only reason for this to not work is: it throws a notice if the array
key is not there (which is the case we are covering anyway)This is basically because the ternary operator does not do a internal
implicit isset, only an empty.Does this seem like a possible improvement we can work on? Anyone
interested in championing the change?
--
Alex Aulbach
On Mon, Jul 23, 2012 at 6:12 AM, Alex Aulbach alex.aulbach@gmail.comwrote:
Do we really need that as operator? Why not using new functions for
special cases.
Don't see much difference between$a = $b ?: $c;
and (for example I used "i" for "if")
$a = _i($b, $c);
Just a manner of getting accustomed to it.
A little bit more thinking: This could be implemented directly into
any program/project, so it is a manner of how to use PHP and not of
why it isn't added to the language.PS: Would it be possible to implement functions like ":?()" or "!?()"
? Currently this is not allowed by syntax.2012/7/18 Rafael Dohms listas@rafaeldohms.com.br:
$width = $config['width'] ?: 300;
The only reason for this to not work is: it throws a notice if the array
key is not there (which is the case we are covering anyway)This is basically because the ternary operator does not do a internal
implicit isset, only an empty.Does this seem like a possible improvement we can work on? Anyone
interested in championing the change?--
Alex Aulbach--
I do think that such operator will be great.
I think that you can compare the situation to the short if syntax ($a > $b
? $c : $d) - you could use a function too that'll do the same thing, but
the syntax is very useful, readable and great.
I think that you can compare the situation to the short if syntax ($a > $b
? $c : $d)
Not sure I understand... that is the situation under discussion,
no?
$a > $b ? ...
and
$a ? ...
both use the ternary operator.
You do raise (maybe on purpose, not totally clear what you were
getting at) the question of whether a more complex (expr1) in one of
these theoretical ternarys w/implicit isset, however it is
implemented, would apply the isset to any variable in (expr1)? That
is, if $a or $b do not exist, does
$a > $b ?? ...
return an error? What if both do not exist?
I think the conclusion (as reached by other people) is that the
operator needs to apply to each individual variable, not to a
multi-step deal like the ternary where there are more cases to
consider.
-- S.
2012/7/23 Sanford Whiteman swhitemanlistens-software@cypressintegrated.com:
I think that you can compare the situation to the short if syntax ($a > $b
? $c : $d)Not sure I understand... that is the situation under discussion,
no?
Use functions. Above case for example:
_greater($a, $b, $c, $d)
where
function _greater($a, $b, $c, $d)
{
if ($a > $b) {
return $c;
}
return $d;
}
My suggestion just is: At any point everybody needs one more operator
for "his stuff". But that's why functions exists.
--
Alex Aulbach
Use functions.
I understood your post; I was replying to Yahav.
Anyway, I don't agree with you that (built-in) functions are all we
can add because there'll be too much debate. Other languages still
consider adding operators over time and/or allow overloading. Getting
functions added to the language is far from easy anyway. And IMO there
are waaaaaay too many places where something bound closely to the
variable like $?a would be instantly useful and portable; adding
functions for each of these would be impossible.
-- S.
On Mon, Jul 23, 2012 at 6:19 AM, Alex Aulbach alex.aulbach@gmail.comwrote:
2012/7/23 Sanford Whiteman <
swhitemanlistens-software@cypressintegrated.com>:I think that you can compare the situation to the short if syntax ($a >
$b
? $c : $d)Not sure I understand... that is the situation under discussion,
no?Use functions. Above case for example:
...
My suggestion just is: At any point everybody needs one more operator
for "his stuff". But that's why functions exists.
This is not about "his stuff", this is about the general userland
expectation of what a ternary operator will do with a $array[key] where it
is not set.
This is a valid use case in every app and as we saw in lots of frameworks.
As for functions, i do not like the idea of having functions around for
this kind of operation, it will bring about the return of "utils.php" and i
don't think that's worth.
So while i agree not everything needs a core implementation, this does not
fall under that case.
--
Alex Aulbach--
--
Rafael Dohms
PHP Evangelist and Community Leader
http://www.rafaeldohms.com.br
http://www.phpsp.org.br
On Sun, Jul 22, 2012 at 9:08 PM, Sanford Whiteman <
swhitemanlistens-software@cypressintegrated.com> wrote:
[...]
You do raise (maybe on purpose, not totally clear what you were
getting at) the question of whether a more complex (expr1) in one of
these theoretical ternarys w/implicit isset, however it is
implemented, would apply the isset to any variable in (expr1)? That
is, if $a or $b do not exist, does$a > $b ?? ...
return an error? What if both do not exist?
My feeling is that either more complex expressions for operators with an
implicit "isset" or "!empty" shouldn't work, or that they should cause
notices. It's a moot point for null-coalescing operators. Since the
proposal is to deal with specific, common usage patterns, it should stay
focused on those patterns unless during development it's revealed that a
more general case is as easy or easier to implement.
My feeling is that either more complex expressions for operators with an
implicit "isset" or "!empty" shouldn't work, or that they should cause
notices.
That's exactly why I think we're going in the wrong direction by
speaking of an "alternate ternary" operator.
You're saying the basic ternary is (expr1) ? (expr2) : (expr3)
But the only notice-free alternate is (var) ?? (expr2) : (expr3)
This alternate is just going to cause confusion as it has a different,
more limited signature.
-- S.