On 11/5/2017 1:03 PM, Niklas Keller wrote:
>>
>> Hi!
>>
>>> My wording was maybe a bit wrong here, and I was biased by the fact that
>>> I would like to see abstract constants. The fact that not everything can
>>
>> What is "abstract constant"? If you need something that can change, just
>> use a method. Constant is meant to be a nice way to write something
>> inherently constant, such as instead of "/very long
>> (and+)cubmber|some?*regexp/" you'd write NICE_REGEXP_CONSTANT. But it's
>> not supposed to create parallel inheritance structure or something. If
>> it needs to be non-constant, just use a method.
>>
> 
> Totally agree with that. We should deprecate constant inheritance instead.
> 
> Regards, Niklas
> 

An abstract constant is a constant that requires its value to be defined
later, like an abstract method that requires its implementation to be
defined later.

The thing is that I want something that CANNOT CHANGE. I want to require
that you define the value, a value that is known at compile time, a
value that is immutable, a value that can never changes during the
program's execution. This is not achievable by current, available means.

What you describe is the intention of the current class constant
implementation of PHP, and some other languages. Pure logic does not
support this claim. The only thing a constant should provide is that its
value is known at compile time. That being said, we already violate that
by supporting dynamic constant definitions via define, but let's not go
down that road here.

Dropping support for constant inheritance is imho also wrong from a pure
logical point of view, since a constant's value is only constant if I
refer to the very same constant. Meaning, the value of a constant in a
subclass must not be the same value as the value in the superclass.

    class Integer extends Number {
        public const MAX = \PHP_INT_MAX;
        public const MIN = \PHP_INT_MIN;
    }

    class WholeNumber extends Integer {
        public const MIN = 0;
    }

    class NaturalNumber extends WholeNumber {
        public const MIN = 1;
    }

We expect that `Integer::MIN` always yields the same value, rightly so,
since it is a constant. However, nobody expects that
`NaturalNumber::MIN` is going to yield the same value as `Integer::MIN`
does, simply because it is a different value. Of course we expect it to
be compatible, they are after all in a tight relationship (inheritance).
They also have to be compatible to each other, otherwise we would
violate the substitutability.

I mentioned Dart in the initial message, they have a much richer
understanding of const than we have it in PHP. Maybe this also helps to
broaden your views on the topic:

https://news.dartlang.org/2012/06/const-static-final-oh-my.html

-- 
Richard "Fleshgrinder" Fussenegger

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to