Hello,

I followed the instructions to mail to this address to measure the reaction
of an idea before going through the process of creating an RFC,
but I am sorry if this is off topic for this board.

Problem:

I have been looking for a solution to reduce the verbosity of my code and
invoke
data type validation exception automatically at the correct point within
the body of methods.

My research lead me to find that type hinting for scalar values is not
easy, mainly due to the impact it has on the language principles
as well as the effect it would have on backwards compatibility.  Examples
found at:
http://nikic.github.io/2012/03/06/Scalar-type-hinting-is-harder-than-you-think.html

Including the RFC:
https://wiki.php.net/rfc/scalar_type_hinting_with_cast

Other solutions I have attempted to find involved mimicking autoboxing:
https://wiki.php.net/rfc/autoboxing

My solution:

In order to reach the best of all worlds, (principles, backwards
compatibility and portability across different user implementations)
I tried to design* a feature where all of these aspects are left in the
hands of the users.

What I came up with was a new construct which shares the same space as
classes, interfaces and traits, with
the sole purpose of being used as a type hint.

You would use it as a type hint, however rather than require the argument
to be an instance of the definition, it processes
the value with a user implemented handler, in order to assert the value or
change it.

My concept can be found here:
https://gist.github.com/orolyn/9ff0756b3cb3cdfe454b

Advantages:
- Methods expecting strict values would no longer need to use inline type
checking for scalar types.
- Users could define reusable type checking for almost any type of value.
- Since the definitions would be user defined, there would be no chance of
conflicts with existing classes.

Disadvantages:
- Assertions would have to be static and stateless if you are using a good
dependency injection pattern.
- Users of libraries incorporating custom type hints may become confused as
to what an argument should be if not properly documented.
- Complications with IDE intellisense (primarily with type detection).

I left out performance issues in the disadvantages because I truthfully
don't know what they would be.

The example I provided is simply a concept.  If the theory is desirable,
then the design is completely reworkable.

Thanks,
Dominic

Reply via email to