On 1 September 2024 17:45:57 BST, Rob Landers <rob@bottled.codes> wrote:
>Is manually copying the default also not type-safe? Is php a type-safe
>language? I think a lot of the arguments I saw suggested that people don't
>review libraries and their implementations when upgrading or installing them.
>This is just a shorthand for manually copy-pasting the default from other
>code, and this argument really only makes sense to me if there are no reviews
>before upgrading/using a library.
Copying and pasting the default value is no different from providing any other
explicit value; whether you choose 69 because you like the number, or because
you saw it was the current default, you are passing an integer. And if you
write 23*3, you're just writing the same integer a different way.
PHP guarantees the type safety of this under inheritance by enforcing
contravariance of input: if you try to write a subclass that would not accept
an integer, when a parent class would, PHP will refuse to compile your
subclass.
Similarly, if the class provides a non-final method like `getDefaultFlags():
int` then it is type safe to call that and assume the value will always be an
integer, because PHP enforces covariance of output: a subclass may not return a
value that would not have been allowed by the parent class.
If you were designing a language where default values were explicitly available
as outputs, you would need to make them covariant, which is option 2.
Obviously, the language cannot directly control the compatibility promises of
third party libraries, but these principles are well enough known that I would
expect popular projects to base their versioning / deprecation policies on them.
Regards,
Rowan Tommins
[IMSoP]