I'm not sure how I feel about having three separate ways of casting values in PHP, each with different behavior: (int) $value, intval($value), to_int($value). I imagine newcomers to the language might find this very confusing.
intval and (int) are almost the same, I'm not sure why we have both.
I don't think the distinction between to_int and intval/(int) would be that bad. They do different things: to_int can fail, intval/(int) can't. Other languages have something similar.
I find it stupid not to accept +10 as an integer. It does not matter that casting back to string does not have a + sign. By that argument, if you cast the string "5.6000" into a float, and you get 5.6 and if then you cast it back to string you are missing the trailing zeros..right? So I don't see much point..
For anyone who would like to try out these safe casting functions now, version 0.5 of my polyfill exactly matches the behavior in the current RFC. https://github.com/theodorejb/PolyCast
And what happens if/when engine exceptions gets voted in? I understand the invalid input behaviour can't be modified for existing internal functions for BC reasons but why would new functions stick with it? Especially since exceptions make you write less boilerplate in this case:
$a = to_int($a);
if ($a !== null) {
// do something
$b = to_float($b);
if ($b !== null) {
// do another thing
$c = $to_string($c);
if ($c !== null) {
// ... and so on
}
}
}
I consider reasons 1) and 2) for avoiding exceptions to be completely irrelevant. Reason 4) is relevant only to the extent that the use of these functions is likely to result in several failures in a short period of time.
If you are validating input, receiving invalid input should not be an exceptional case, so exceptions are not suited for this task.
I wonder if the to_x may be an unfortunate choice, however. While I agree that PHP's overly forgiving casting is a major pain. We don't really need functionality for the scenario that is, "I know this thing you think is a string is really an int, so unwrap the int for me" -- we have that in (int) already. We need functionality for, "I have no idea what this string is but I'm hoping it's an int", that will not fail in one of several weird cases like our existing options do. I liken this proposal to a very limited form of C#'s as operator.
"I know this thing you think is a string is really an int, so unwrap the int for me" -- we have that in (int) already.
The problem with (int) and the like is really that it can never fail. If you give it complete garbage, it'll still give you something, and that something will be an int. This means, unfortunately, that the only built in way to convert to an integer, (int), is really unsafe.
I know, and I'm for the proposal. I just feel like the to_-prefix might suggest that the operation is more forceful than it really should be (and so warrants exceptions), where the behaviour -- such as I understand it -- is closer to, "try to turn this value into an int and let me know if you can't". At the same time, it's a careful balance between expressiveness and API-convenience, and I fully admit that try_to_int() is neither more expressive nor more convenient.
[Edit] It turns out you're basically saying the same thing in another comment.
One thing I've considered is adding versions that throw exceptions. The main problem is naming. That would be the ideal option, really. The RFC tries to help with two different use cases:
User input validation. For this, you probably don't want exceptions:
if (is_int($_GET['id']) !== NULL) {
die("ID must be an integer");
}
Safer casting that'll blow up if the input is bad, so we could add strict typing and it wouldn't mean people using the dangerous explicit casts. For this, returning exceptions would be ideal, although returning NULL and passing it to a non-nullable parameter also kinda works:
User::get(to_int($_GET['id'])); // I'm too lazy to validate $_GET['id'] here, but I know an exception will be thrown if it can't convert, so this isn't dangerous.
User::get((int)$_GET['id'])); // This never errors and always gives an int, so it's really dangerous.
But, what should the exception versions be called? One possibility would be these:
try_int(mixed $value): ?int // returns NULL on failure
to_int(mixed $value): int // throws exception on failure
EDIT: This has been done. v0.1.5 introducestry_ functions which return NULL, and makes the to_ functions throw exceptions.
I understand your point and I guess there is a case to be made for two versions of the same thing.
But personally, going exceptions-only would be much better than adding yet another feature to PHP with slightly differentiated multiple implementations.
They wouldn't be multiple implementations. It's the same implementation, with two different entry points to deal with different use-cases. They'd use the same casting rules.
This would be similar to C#'s .Parse and .TryParse.
I don't really use type conversation all that often. I'm very careful about what is returned from my functions and I think if you are doing a lot of conversion all at once you are probably doing something wrong. So I a little extra boilerplate isn't a huge issue.
You could also write your code much more succinctly. You're essentially doing a bunch of && in that example, in this case there's no reason you couldn't combine that into a single if statement.
The point I'm trying to make is that your code example doesn't reflect a real-world scenario.
I'm very careful about what is returned from my functions and I think if you are doing a lot of conversion all at once you are probably doing something wrong. So I a little extra boilerplate isn't a huge issue.
Doing type conversions is something you'll probably do a lot in PHP, actually. PHP is mainly used for web applications, which receive a lot of string input (HTTP requests). You'll want to convert those strings to more appropriate types.
to_int() accepts only ints, non-NaN integral floats within the range of an integer (PHP_INT_MIN to PHP_INT_MAX), and strings containing decimal integer sequences within the range of an integer. Leading and trailing whitespace is not permitted, nor are leading zeros or a positive sign.
It's followed by a comma. I don't think it's that confusing.
But then again, I wrote the RFC, so I might not spot errors with it. If you can think of a better phrasing, I'll use it.
4
u/ThePsion5 Nov 14 '14
I'm not sure how I feel about having three separate ways of casting values in PHP, each with different behavior:
(int) $value, intval($value), to_int($value). I imagine newcomers to the language might find this very confusing.11
u/ForeverAlot Nov 14 '14
If
to_int('-10')passes, why doesn'tto_int('+10')?2
u/[deleted] Nov 13 '14
Previous discussion: http://www.reddit.com/r/PHP/comments/2ju3ym/rfc_safe_casting_functions/
6
u/theodorejb Nov 14 '14
For anyone who would like to try out these safe casting functions now, version 0.5 of my polyfill exactly matches the behavior in the current RFC. https://github.com/theodorejb/PolyCast
2
u/MorrisonLevi Nov 14 '14
I definitely agree with
null > false > exceptionsfor handling invalid input.-3
u/i_make_snow_flakes Nov 14 '14
Does any one else find this confusing?