# GMP object and is_scalar()

- Thread: https://externals.io/message/70904
- Original archive: https://news-web.php.net/php.internals/70904
- Started by Yasuo Ohgaki on 2013-12-29T08:09:50+00:00
- Messages: 17
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #70904 — Yasuo Ohgaki — 2013-12-29T08:09:50+00:00

URL: https://externals.io/message/70904#70904

Hi all,

GMP object is object. Therefore, `is_scalar()` returns false for GMP
objects.

[yohgaki@dev php-src]$ ./php-bin -r '$v = gmp_init("0");
var_dump(gettype($v));'
string(6) "object"
[yohgaki@dev php-src]$ ./php-bin -r '$v = gmp_init("0");
var_dump(is_scalar($v));'
bool(false)

This is correct behavior, but this behavior is debatable, IMO.

Any comments?

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #70905 — Yasuo Ohgaki — 2013-12-29T08:16:20+00:00

URL: https://externals.io/message/70904#70905 · In reply to: #70904

> GMP object is object. Therefore, `is_scalar()` returns false for GMP
> objects.
>
> [yohgaki@dev php-src]$ ./php-bin -r '$v = gmp_init("0");
> var_dump(gettype($v));'
> string(6) "object"
> [yohgaki@dev php-src]$ ./php-bin -r '$v = gmp_init("0");
> var_dump(is_scalar($v));'
> bool(false)
>
> This is correct behavior, but this behavior is debatable, IMO.
>
> Any comments?
>

 `is_numeric()` can be problematic also.

[yohgaki@dev php-src]$ ./php-bin -r '$v = gmp_init("0");
var_dump(is_numeric($v));'
bool(false)

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #70907 — Nikita Popov — 2013-12-29T17:54:30+00:00

URL: https://externals.io/message/70904#70907 · In reply to: #70904

> Hi all,
>
> GMP object is object. Therefore, `is_scalar()` returns false for GMP
> objects.
>
> [yohgaki@dev php-src]$ ./php-bin -r '$v = gmp_init("0");
> var_dump(gettype($v));'
> string(6) "object"
> [yohgaki@dev php-src]$ ./php-bin -r '$v = gmp_init("0");
> var_dump(is_scalar($v));'
> bool(false)
>
> This is correct behavior, but this behavior is debatable, IMO.
>
> Any comments?
>

Sorry, I don't understand what you're saying. Why would is_scalar return
true for an **object**?

Nikita

---

## #70908 — Hannes Magnusson — 2013-12-29T19:26:26+00:00

URL: https://externals.io/message/70904#70908 · In reply to: #70907

> 
>
> > […]
>
> Sorry, I don't understand what you're saying. Why would is_scalar return
> true for an **object**?


Its not even an object, its a resource.

I think you are completely off path here Yasuo, same with your empty() thread.
That just doesn't make sense.

-Hannes

---

## #70909 — Hannes Magnusson — 2013-12-29T19:33:43+00:00

URL: https://externals.io/message/70904#70909 · In reply to: #70908

On Sun, Dec 29, 2013 at 11:26 AM, Hannes Magnusson
<hannes.magnusson@gmail.com> wrote:
> 
> > […]
>
>
> Its not even an object, its a resource.
>
> I think you are completely off path here Yasuo, same with your empty() thread.
> That just doesn't make sense.


Waiiit a minute. It is a object in current master?

That will break code that makes sure gmp_init() worked by doing
 `is_resource()` on the return value.

-Hannes

---

## #70911 — Yasuo Ohgaki — 2013-12-29T22:12:22+00:00

URL: https://externals.io/message/70904#70911 · In reply to: #70907

Hi Nikita,



> Sorry, I don't understand what you're saying. Why would is_scalar return
> true for an **object**?


Since GMP object works just like numbers, this could cause bug that
users assume it is "number" or "scalar".

Variables are promoted to GMP object when math operator is applied.
(This is nice, IMO)  GMP objects works like number mostly, this can be
cause of bugs in user scripts.

It would be more intuitive for users if GMP object is handled as "number"
IMHO.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #70927 — Stas Malyshev — 2013-12-30T20:45:58+00:00

URL: https://externals.io/message/70904#70927 · In reply to: #70911

Hi!

> Since GMP object works just like numbers, this could cause bug that
> users assume it is "number" or "scalar".

is_scalar checks if the value is a scalar. Objects and resources are not
scalars. If someone doesn't understand that, he should read manual page
of is_scalar before using it.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #70930 — Yasuo Ohgaki — 2013-12-30T21:20:18+00:00

URL: https://externals.io/message/70904#70930 · In reply to: #70927

Hi Stas,

On Tue, Dec 31, 2013 at 5:45 AM, Stas Malyshev <smalyshev@sugarcrm.com>wrote:

> > […]
>
> is_scalar checks if the value is a scalar. Objects and resources are not
> scalars. If someone doesn't understand that, he should read manual page
> of is_scalar before using it.


I agree. Generally speaking, users should know this.

I'm bringing  up this as issue to discuss, since GMP numbers will be work
like numbers from PHP 5.6. Most users will expect numbers are evaluated
like other numbers(int/float), I suppose.

It may be hard to distinguish normal numbers(int/float) and GMP numbers
in large scripts, too.

I can think of 3 options for now.

 - Treat GMP object as special object
 - Make scalar object
 - Make autoboxing like feature

We have 4th option that leave it as it now, but it wouldn't be user
friendly.
Since this (GMP numbers work like normal numbers) is new feature, we
may choose better option.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #70931 — Stas Malyshev — 2013-12-30T21:31:07+00:00

URL: https://externals.io/message/70904#70931 · In reply to: #70930

Hi!

> Since this (GMP numbers work like normal numbers) is new feature, we 
> may choose better option.

I think you're taking it way too far. GMP numbers may support some
functions that scalars support, but that does not mean every function -
especially functions with are *specifically designed* to distinguish
scalars from objects - would do the same thing for them as for scalars.
If you're interested in their "numeric" properties - i.e. adding them,
multiplying them, etc. - fine, they work that way. But if you "pop the
hood" and look inside - no, they should not be scalars inside, because
they are not.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #70932 — Yasuo Ohgaki — 2013-12-30T21:35:57+00:00

URL: https://externals.io/message/70904#70932 · In reply to: #70931

Hi Stas,

On Tue, Dec 31, 2013 at 6:31 AM, Stas Malyshev <smalyshev@sugarcrm.com>wrote:

> > […]
>
> I think you're taking it way too far. GMP numbers may support some
> functions that scalars support, but that does not mean every function -
> especially functions with are *specifically designed* to distinguish
> scalars from objects - would do the same thing for them as for scalars.
> If you're interested in their "numeric" properties - i.e. adding them,
> multiplying them, etc. - fine, they work that way. But if you "pop the
> hood" and look inside - no, they should not be scalars inside, because
> they are not.


I agree with your opinion in general.

How about to introduce scalar object that is evaluated as scalar?
It would be useful in user land also.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #70933 — Yasuo Ohgaki — 2013-12-30T21:46:07+00:00

URL: https://externals.io/message/70904#70933 · In reply to: #70932

Hi Stas,



> On Tue, Dec 31, 2013 at 6:31 AM, Stas Malyshev <smalyshev@sugarcrm.com>wrote:
>
> > […]
>
>
> I agree with your opinion in general.
>
> How about introduce scalar object that is evaluated as scalar?
> It would be useful in user land also.
>

http://www.php.net/manual/en/language.oop5.magic.php

Something like __toScalar(), may be?

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #70934 — Robert Stoll — 2013-12-30T21:55:53+00:00

URL: https://externals.io/message/70904#70934 · In reply to: #70933

> -----Original Message-----
> From: yohgaki@gmail.com [mailto:yohgaki@gmail.com] On Behalf Of Yasuo Ohgaki
> Sent: Monday, December 30, 2013 10:46 PM
> To: Stas Malyshev
> Cc: Nikita Popov; internals@lists.php.net
> Subject: Re: [PHP-DEV] GMP object and `is_scalar()`
> 
> Hi Stas,
> 
> 
> 
> > […]
> 
> http://www.php.net/manual/en/language.oop5.magic.php
> 
> Something like __toScalar(), may be?
> 
> --
> Yasuo Ohgaki
> yohgaki@ohgaki.net

I think __toScalar is way to wage, what will __toScalar() return? a bool, an int, a float or a string?

---

## #70935 — Yasuo Ohgaki — 2013-12-30T22:00:14+00:00

URL: https://externals.io/message/70904#70935 · In reply to: #70934

Hi Robert,



> I think __toScalar is way to wage, what will __toScalar() return? a bool,
> an int, a float or a string?


Not all classes should have __toScalar().
Only classes that should be evaluated as scalar should implement this magic
method.

Return values are programmer's choice.
Whatever scalar values may return.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #70936 — Stas Malyshev — 2013-12-30T22:03:51+00:00

URL: https://externals.io/message/70904#70936 · In reply to: #70935

Hi!

> Only classes that should be evaluated as scalar should implement this
> magic method.

We already have type convertors. "scalar" is not a type, it's a group of
types. I don't think it makes sense to convert something to an abstract
scalar - it should be specific scalar type.

-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #70937 — Yasuo Ohgaki — 2013-12-30T22:12:05+00:00

URL: https://externals.io/message/70904#70937 · In reply to: #70936

Hi Stas,

On Tue, Dec 31, 2013 at 7:03 AM, Stas Malyshev <smalyshev@sugarcrm.com>wrote:

> > […]
>
> We already have type convertors. "scalar" is not a type, it's a group of
> types. I don't think it makes sense to convert something to an abstract
> scalar - it should be specific scalar type.


Should it? PHP is weakly typed language.
I haven't thought well for scalar object yet and has to consider
various cases.

One thing clear to me is users expects GMP numbers behave like
scalar numbers most likely.

Yet another option for PHP 5.6 is to note GMP numbers as
EXPERIMENTAL.

There are many issues to resolve. IMHO.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #70938 — Stas Malyshev — 2013-12-30T22:20:23+00:00

URL: https://externals.io/message/70904#70938 · In reply to: #70937

Hi!

> Should it? PHP is weakly typed language.

That doesn't mean types do not exist. That means values can be converted
from type to type, but there's no type "scalar" - it is just a group
name of a set of types.

> One thing clear to me is users expects GMP numbers behave like 
> scalar numbers most likely.

I don't think users expect anything like that, I don't see legitimate
use case why users would expect is_scalar return true on GMP object. If
the user is advanced enough to use is_scalar he should understand what
that function does. Or user some other function that is more fit for
their goals.

> Yet another option for PHP 5.6 is to note GMP numbers as
> EXPERIMENTAL.

GMP is in PHP for a long time, why would it suddenly become experimental?

-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

---

## #70939 — Yasuo Ohgaki — 2013-12-30T22:49:51+00:00

URL: https://externals.io/message/70904#70939 · In reply to: #70938

Hi Stas,

On Tue, Dec 31, 2013 at 7:20 AM, Stas Malyshev <smalyshev@sugarcrm.com>wrote:

> > […]
>
> That doesn't mean types do not exist. That means values can be converted
> from type to type, but there's no type "scalar" - it is just a group
> name of a set of types.
>

Agreed on that scalar is name of types which has single value.

 > One thing clear to me is users expects GMP numbers behave like
> > […]
>
> I don't think users expect anything like that, I don't see legitimate
> use case why users would expect is_scalar return true on GMP object. If
> the user is advanced enough to use is_scalar he should understand what
> that function does. Or user some other function that is more fit for
> their goals.
>

I agree users who know PHP and GMP well would not expect GMP object
is to evaluated as scalar.

I suppose average users would expect/want GMP number as PHP
number also.

> Yet another option for PHP 5.6 is to note GMP numbers as
> > […]
>
> GMP is in PHP for a long time, why would it suddenly become experimental?


Sorry, I meant GMP number arithmetic with normal operators/math functions/
type evaluation functions is better to be considered as EXPERIMENTAL.

It lacks GMP float support and many function won't work as it should be.
(If we consider GMP number as "number" in PHP)

It's safe to note GMP number arithmetic as EXPERIMENTAL and
resolve issues. IMHO.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net
