Re: [PHP-DEV] [RFC] Constant Scalar Expressions

2013-08-14 Thread Stas Malyshev
Hi!

> Perhaps something for OpCache's optimizer though, if it doesn't already 
> do that...

The problem with that is once you start to do conversions, things quicky
go south, as conversions can depend on runtime variables, and people get
really weird bugs when their expressions are not evaluated in the same
time always.

In static context, like with proposed patch, nobody has expectations so
saying "it all happens compile-time, runtime settings do not apply" is
fine. But when you already have running code that works certain way,
changing it may be dangerous.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP-DEV] [RFC] Constant Scalar Expressions

2013-08-14 Thread Johannes Schlüter
On Wed, 2013-08-14 at 12:03 +0100, Derick Rethans wrote:
> On Wed, 14 Aug 2013, Patrick Schaaf wrote:
> 
> > Would this allow using constants, too? Class constants?
> > 
> > const FOO = 1;
> > const BAR = self::FOO + 1;
> > const BAZ = self::FOO + 2;
> > const BARF = GLOBAL_BARF;
> > const IMPORT = otherclass::IMPORT; // with autoloading?
> > 
> > In my opinion these would start to make the feature useful.
> 
> Those are not constants, but expressions which can't be run during 
> compile time.

We already support constants where we calculate the value at runtime via
ZEND_DECLARE_CONST opcode. i.e.
 class A { const C1 = 1; const C2 = A::C1; }
is valid. If we allow scalar arithmetic we should extend this, too to be
consistent in the language.

johannes



-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] [RFC] Constant Scalar Expressions

2013-08-14 Thread Bob Weinand
Am 13.08.2013 um 14:13 schrieb "Anthony Ferrara" :

> Hello all,
> 
> I'd like to propose a new RFC for 5.NEXT:
> 
> https://wiki.php.net/rfc/const_scalar_expressions
> 
> This allows for defining constant expressions which are resolved at compile
> time.
> 
> for example:
> 
> const FOO = 1 + 1;
> static $bar = 1 << 2;
> function foo($a = 1 | 2) {}
> class foo {
>public $bar = 1 << 2;
> }
> 
> Thoughts?
> 
> Anthony

It really would be nice to use constants in these constant expressions.

I think this would be particulary useful when using bitwise operators. Example:

const FLAG_A = 1;
const FLAG_B = 2;
const FLAG_C = 4;

With this proposal we would write:
function func ($arg = 1 | 2 | 4) # we now need to lookup first which constants 
have values 1, 2 and 4

But it would be much more understandable if we were able to write:
function func ($arg = FLAG_A | FLAG_B | FLAG_C)

Bob Weinand
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] [RFC] Constant Scalar Expressions

2013-08-14 Thread Lars Strojny
Super cool, thanks!

Am 13.08.2013 um 18:12 schrieb Anthony Ferrara :

> Hello all,
> 
> I'd like to propose a new RFC for 5.NEXT:
> 
> https://wiki.php.net/rfc/const_scalar_expressions
> 
> This allows for defining constant expressions which are resolved at compile
> time.
> 
> for example:
> 
> const FOO = 1 + 1;
> static $bar = 1 << 2;
> function foo($a = 1 | 2) {}
> class foo {
>public $bar = 1 << 2;
> }
> 
> Thoughts?
> 
> Anthony



signature.asc
Description: Message signed with OpenPGP using GPGMail


Re: [PHP-DEV] [RFC] Constant Scalar Expressions

2013-08-14 Thread Derick Rethans
On Wed, 14 Aug 2013, Anthony Ferrara wrote:

> Sebastian,
> 
> 
> On Wed, Aug 14, 2013 at 7:44 AM, Sebastian Krebs wrote:
> 
> > Just asking: Does this cover only declarations, or every constant 
> > expression, for example
> >
> > $weeks = $secs / (60 * 60 * 24 * 7);
> >
> > becomes to the opcode-equivalent of
> >
> > $weeks = $secs  / (604800);

A week isn't always 7 * 24 hours...

> Currently, only places that use the static_scalar parser definition will
> use this: http://lxr.php.net/xref/PHP_TRUNK/Zend/zend_language_parser.y#945
> 
> So today that's constants (using const keyword), class constants, class
> properties, function parameter values and declare statements.
> 
> To do the other part would likely need to happen in the compiler itself
> (look for all opcodes of type _SPEC_CONST_CONST, and then optimize those
> away). I will experiment a bit, but my gut tells me it will result in a lot
> of parser or compiler complexity to attempt to do that (for which there
> will be so little gain, as the runtime performance is already quite
> fast)... At which point the complexity isn't worth it.

Perhaps something for OpCache's optimizer though, if it doesn't already 
do that...

cheers,
Derick

-- 
http://derickrethans.nl | http://xdebug.org
Like Xdebug? Consider a donation: http://xdebug.org/donate.php
twitter: @derickr and @xdebug
Posted with an email client that doesn't mangle email: alpine

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] [RFC] Constant Scalar Expressions

2013-08-14 Thread Anthony Ferrara
Sebastian,


On Wed, Aug 14, 2013 at 7:44 AM, Sebastian Krebs wrote:

> Hi,
>
> Just asking: Does this cover only declarations, or every constant
> expression, for example
>
> $weeks = $secs / (60 * 60 * 24 * 7);
>
> becomes to the opcode-equivalent of
>
> $weeks = $secs  / (604800);
>
> ?
>

Currently, only places that use the static_scalar parser definition will
use this: http://lxr.php.net/xref/PHP_TRUNK/Zend/zend_language_parser.y#945

So today that's constants (using const keyword), class constants, class
properties, function parameter values and declare statements.

To do the other part would likely need to happen in the compiler itself
(look for all opcodes of type _SPEC_CONST_CONST, and then optimize those
away). I will experiment a bit, but my gut tells me it will result in a lot
of parser or compiler complexity to attempt to do that (for which there
will be so little gain, as the runtime performance is already quite
fast)... At which point the complexity isn't worth it.

Anthony


Re: [PHP-DEV] [RFC] Constant Scalar Expressions

2013-08-14 Thread Sebastian Krebs
Hi,

Just asking: Does this cover only declarations, or every constant
expression, for example

$weeks = $secs / (60 * 60 * 24 * 7);

becomes to the opcode-equivalent of

$weeks = $secs  / (604800);

?


2013/8/14 Anthony Ferrara 

> Stas,
>
>
> On Wed, Aug 14, 2013 at 5:01 AM, Stas Malyshev  >wrote:
>
> > Hi!
> >
> > >> https://wiki.php.net/rfc/const_scalar_expressions
> >
> > I like the idea, but absence of constant support makes this thing much
> > less useful, as you can't do things like:
> >
> > public $angle = M_PI/2;
> >
> > I think this is one of the reasons this idea was never implemented -
> > because without constant support you're limited to doing things that are
> > quite obvious and trivial.
> >
>
> Yeah, having constants in those expressions would be great. If only
> constants in PHP were actually constant...
>
> But this win is really cheap (a trivial change to the parser), so I figured
> it was worth proposing separately. If we want to add the opcode stream
> later to do expressions for constant values, we can. This just gives us the
> quick win today of allowing relatively trivial, but important expressions.
>
> The biggest wins I see are in power-of-2 math:
>
> class Foo {
> const FLAG_1 = 1 << 0;
> const FLAG_2 = 1 << 1;
> const FLAG_3 = 1 << 2;
> const FLAG_4 = 1 << 3;
> const FLAG_5 = 1 << 4;
> const FLAG_6 = 1 << 5;
> const FLAG_7 = 1 << 6;
> }
>
> And in other complex formulas where having the self-declaration adds
> semantic meaning.
>
> Now, as far as if it's worth while making the change without constant
> support, that's for each of us to decide. I think it is, but if you don't,
> that's cool too.
>
> Thanks
>
> Anthony
>



-- 
github.com/KingCrunch


RE: [PHP-DEV] [RFC] Constant Scalar Expressions

2013-08-14 Thread Patrick Schaaf
Am 14.08.2013 13:03 schrieb "Derick Rethans" :
>
> On Wed, 14 Aug 2013, Patrick Schaaf wrote:
>
> > Would this allow using constants, too? Class constants?
> >
> > const FOO = 1;
> > const BAR = self::FOO + 1;
>
> Those are not constants, but expressions which can't be run during
> compile time.

Understood now. Constants are not constants.

Is there a general consensus that just-in-time at-runtime initialization in
these places (as well as for property initializers) is not possible or
desired - or is that just something that would need a lot of thought and
work?

best regards
  Patrick


RE: [PHP-DEV] [RFC] Constant Scalar Expressions

2013-08-14 Thread Derick Rethans
On Wed, 14 Aug 2013, Patrick Schaaf wrote:

> Would this allow using constants, too? Class constants?
> 
> const FOO = 1;
> const BAR = self::FOO + 1;
> const BAZ = self::FOO + 2;
> const BARF = GLOBAL_BARF;
> const IMPORT = otherclass::IMPORT; // with autoloading?
> 
> In my opinion these would start to make the feature useful.

Those are not constants, but expressions which can't be run during 
compile time.

Derick

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] [RFC] Constant Scalar Expressions

2013-08-14 Thread Julien Pauli
On Wed, Aug 14, 2013 at 12:44 PM, Anthony Ferrara wrote:

> Stas,
>
>
> On Wed, Aug 14, 2013 at 5:01 AM, Stas Malyshev wrote:
>
>> Hi!
>>
>> >> https://wiki.php.net/rfc/const_scalar_expressions
>>
>> I like the idea, but absence of constant support makes this thing much
>> less useful, as you can't do things like:
>>
>> public $angle = M_PI/2;
>>
>> I think this is one of the reasons this idea was never implemented -
>> because without constant support you're limited to doing things that are
>> quite obvious and trivial.
>>
>
> Yeah, having constants in those expressions would be great. If only
> constants in PHP were actually constant...
>
> But this win is really cheap (a trivial change to the parser), so I
> figured it was worth proposing separately. If we want to add the opcode
> stream later to do expressions for constant values, we can. This just gives
> us the quick win today of allowing relatively trivial, but important
> expressions.
>
> The biggest wins I see are in power-of-2 math:
>

I agree !


>
> class Foo {
> const FLAG_1 = 1 << 0;
> const FLAG_2 = 1 << 1;
> const FLAG_3 = 1 << 2;
> const FLAG_4 = 1 << 3;
> const FLAG_5 = 1 << 4;
> const FLAG_6 = 1 << 5;
> const FLAG_7 = 1 << 6;
> }
>
>
Julien


Re: [PHP-DEV] [RFC] Constant Scalar Expressions

2013-08-14 Thread Anthony Ferrara
Stas,


On Wed, Aug 14, 2013 at 5:01 AM, Stas Malyshev wrote:

> Hi!
>
> >> https://wiki.php.net/rfc/const_scalar_expressions
>
> I like the idea, but absence of constant support makes this thing much
> less useful, as you can't do things like:
>
> public $angle = M_PI/2;
>
> I think this is one of the reasons this idea was never implemented -
> because without constant support you're limited to doing things that are
> quite obvious and trivial.
>

Yeah, having constants in those expressions would be great. If only
constants in PHP were actually constant...

But this win is really cheap (a trivial change to the parser), so I figured
it was worth proposing separately. If we want to add the opcode stream
later to do expressions for constant values, we can. This just gives us the
quick win today of allowing relatively trivial, but important expressions.

The biggest wins I see are in power-of-2 math:

class Foo {
const FLAG_1 = 1 << 0;
const FLAG_2 = 1 << 1;
const FLAG_3 = 1 << 2;
const FLAG_4 = 1 << 3;
const FLAG_5 = 1 << 4;
const FLAG_6 = 1 << 5;
const FLAG_7 = 1 << 6;
}

And in other complex formulas where having the self-declaration adds
semantic meaning.

Now, as far as if it's worth while making the change without constant
support, that's for each of us to decide. I think it is, but if you don't,
that's cool too.

Thanks

Anthony


Re: [PHP-DEV] [RFC] Constant Scalar Expressions

2013-08-14 Thread Michael Wallner
On 14 August 2013 11:01, Stas Malyshev  wrote:
> Hi!
>
>>> https://wiki.php.net/rfc/const_scalar_expressions
>
> I like the idea, but absence of constant support makes this thing much
> less useful, as you can't do things like:
>
> public $angle = M_PI/2;
>
> I think this is one of the reasons this idea was never implemented -
> because without constant support you're limited to doing things that are
> quite obvious and trivial.

Yeah, I'm generally +1 on the idea; constant support would be awesome though!


-- 
Regards,
Mike

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] [RFC] Constant Scalar Expressions

2013-08-14 Thread Patrick ALLAERT
2013/8/13 Anthony Ferrara :
> Hello all,
>
> I'd like to propose a new RFC for 5.NEXT:
>
> https://wiki.php.net/rfc/const_scalar_expressions
>
> This allows for defining constant expressions which are resolved at compile
> time.
>
> for example:
>
> const FOO = 1 + 1;
> static $bar = 1 << 2;
> function foo($a = 1 | 2) {}
> class foo {
> public $bar = 1 << 2;
> }
>
> Thoughts?
>
> Anthony

Since the RFC specifically avoid the possibility to use non static values: +1

Cheers,
Patrick

-- 
Patrick Allaert
---
http://code.google.com/p/peclapm/ - Alternative PHP Monitor

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] [RFC] Constant Scalar Expressions

2013-08-14 Thread Stas Malyshev
Hi!

>> https://wiki.php.net/rfc/const_scalar_expressions

I like the idea, but absence of constant support makes this thing much
less useful, as you can't do things like:

public $angle = M_PI/2;

I think this is one of the reasons this idea was never implemented -
because without constant support you're limited to doing things that are
quite obvious and trivial.
-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] [RFC] Constant Scalar Expressions

2013-08-14 Thread Sebastian Bergmann
Am 13.08.2013 18:12, schrieb Anthony Ferrara:
> Thoughts?

 +1 :)

-- 
Sebastian BergmannCo-Founder and Principal Consultant
http://sebastian-bergmann.de/   http://thePHP.cc/

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-DEV] [RFC] Constant Scalar Expressions

2013-08-14 Thread Julien Pauli
On Tue, Aug 13, 2013 at 6:12 PM, Anthony Ferrara wrote:

> Hello all,
>
> I'd like to propose a new RFC for 5.NEXT:
>
> https://wiki.php.net/rfc/const_scalar_expressions
>
> This allows for defining constant expressions which are resolved at compile
> time.
>
> for example:
>
> const FOO = 1 + 1;
> static $bar = 1 << 2;
> function foo($a = 1 | 2) {}
> class foo {
> public $bar = 1 << 2;
> }
>
> Thoughts?
>

Haha Anthony, so strange you think about this point I was just myself
figuring out yesterday.

I'am obviously +1.

Julien Pauli.


Re: [PHP-DEV] [RFC] Constant Scalar Expressions

2013-08-14 Thread Pierre Joye
On Wed, Aug 14, 2013 at 8:17 AM, Christian Stoller  wrote:
>> Hello all,
>>
>> I'd like to propose a new RFC for 5.NEXT:
>>
>> https://wiki.php.net/rfc/const_scalar_expressions
>>
>> This allows for defining constant expressions which are resolved at compile
>> time.
>
> What should that be for?
>
>> const FOO = 1 + 1;
>> const BAZ = "HELLO " . "WORLD!";
>
> Why not just writing
>
> const FOO = 2;
> const BAZ = "HELLO WORLD!";
>
> I think it makes code les readable. And if you want to give an important hint 
> for the reader of the code, you can still write comments.

When a constant is based on a complex formula, it makes a lot of sense
to keep it as it is defined or known. I had many projects (finance,
stats and BI related areas) where I would have used constant
expressions. As you suggested comments work fine, but do you know what
my comments were? The expressions used to generate the constant,
sounds double with little to no gain to me.

Summary: huge +1 on that.

-- 
Pierre

@pierrejoye | http://www.libgd.org

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php