On 11/6/2017 1:44 AM, Stanislav Malyshev wrote: > Hi! > >> 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. > > It's not a constant then, and should be a method. >
No, because a method can change what it returns, it is not constant. On 11/6/2017 1:44 AM, Stanislav Malyshev wrote: >> >> The thing is that I want something that CANNOT CHANGE. I want to require > > You are contradicting yourself. If it is not known upfront, then it can > change - otherwise, you'd know it upfront. I'm not sure what you're > trying to do here, but I am getting pretty sure you shouldn't be doing > it with const's :) > No, I want to split the definition and initialization. The value cannot change, once it is initialized. On 11/6/2017 1:44 AM, Stanislav Malyshev wrote: >> 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; >> } > > Integer::MIN and NaturalNumber::MIN are different constants. So, it is > natural that they can have different values. Though using constants with > these names is slightly misleading, but if you always use full name, not > by much. > I do not follow, what is misleading about them? Referencing them without the full name is also not possible in PHP, hence, not an issue. On 11/6/2017 1:44 AM, Stanislav Malyshev wrote: >> 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). > > Here you are getting into a dangerous territory, btw. Depending on your > modeling needs, of course, but your NaturalNumber can violate contract > of Integer, such as "being able to represent -10". Thus, inheritance > could be wrong way to do it, at least in the way you described above. > One has to be very careful with which exactly contract are you modelling > - inheritance is not just shortcut for avoiding copy-paste. > I am not sure how you deduce that NaturalNumber can be -10. There are not invariants defined anywhere on the classes. You are correct that inheritance should not be misused for code reuse, it should be used to build type systems. Which is exactly what I am doing in the example. On 11/6/2017 1:44 AM, Stanislav Malyshev wrote: >> 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 > > 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. -- Richard "Fleshgrinder" Fussenegger
signature.asc
Description: OpenPGP digital signature