The [type]p() functions all test the value in a variable, as does _typeof(), which returns a type value:
int|float x = 0.0; intp(x);
(1) Result: 0
int|float x = 0; intp(x);
(2) Result: 1
_typeof(x);
(3) Result: zero
x = 1.0;
(4) Result: 1.0
_typeof(x);
(5) Result: float
If you want to test the variable type, you can use typeof(), which returns the type of a given something rather than the type of the thing it holds:
typeof(x);
(6) Result: int | float
Type types are pretty interesting, but I’m not sure that there’s a good way to express a type literal for use in comparisons. If, for some reason, you need to see if a variable can hold a given type, you can do something like:
// can x hold a float?
mixed y = 1.0;
// note that we compare the variable type of x with the type of the value held in y, // that way we can change the value in y to test for different types. // // if we just wanted a static comparison, we could typeof() against a variable of the // type we wanted. Also, note that 0 gets a type of its own (zero)
(typeof(x) & _typeof(y)) == _typeof(y);
(8) Result: 1
// can x hold a string?
y = ""; (typeof(x) & _typeof(y)) == _typeof(y);
(10) Result: 0
Maybe grubba can fill in any missing pieces (such as if there’s a way to create type values directly).
Bill
On Apr 8, 2018, at 10:59 AM, Lance Dillon riffraff169@yahoo.com wrote:
Just out of curiosity, how does intp() and such work.
Say you have int|float x, then x=5.0.
Does intp test the type of the variable, or the type of the contents. Does intp(x) return true because x is of type int (also of type float), or false because the contents of x (currently 5.0) is float, so not an int at that point.
I'm thinking contents, not variable itself, so you can do checks against it.
Sent from Yahoo Mail on Android