On 11/11/2017 18:39, Fleshgrinder wrote:
On 11/6/2017 1:44 AM, Stanislav Malyshev wrote:

 From this link, it looks like const in Dart has pretty much nothing in
common with const in PHP, besides name, so in the interest of avoiding
confusion, I would not discuss it in the same topic.

Yes, Dart has a different understanding of const, which is exactly why I
posted it for you guys. In the hope that it helps to get more different
views on the topic. Currently you are too concentrated on how it is
implemented in PHP at this time, and argue that it is impossible to
diverge from that path. Which is simply not true, we only have to ensure
backwards compatibility.

I think the point is that adding "single-assignment variables" (which Dart calls "final") or "deeply immutable values" (which Dart calls "const") would be a completely different feature which could be added side-by-side with what we have now, under a different name. What PHP calls "const" (in the context of classes) is neither of those features.


The debate seems to be whether you view class constants as more like static properties, or static methods. Given this:

class A { public const Foo = 42; }
echo A::Foo;

Is it equivalent to this (using an imaginary "readonly" modifier)...

class A { public static readonly $foo = 42; }
echo A::$foo;

...or is it equivalent to this (particularly if you imagine an optimising compiler that caches / inlines the result)?

class A { public static function foo(): int { return 42; } }
echo A::foo();


The difference is that a field is never "abstract" - it either has a value, or it is undefined; you can't add a contract to an interface saying "you must have this field" either. A method, on the other hand, is assumed to encapsulate something - it's a black box with a contract, so defining the contract without any implementation makes sense. (Note that I've called $foo a "field" to distinguish it from a "property", as C# does: a property with a contract like "$foo { public get; private set; }" could indeed be abstract.)

The interesting thing about the above examples is that the static method with a fixed return *already works right now*, whereas the readonly field doesn't exist; so it makes some sense to say that "const FOO" is PHP's way of saying "static readonly $FOO", and have it subject to similar semantics.

Regards,

--
Rowan Collins
[IMSoP]


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

Reply via email to