📜 ⬆️ ⬇️

Adaptive typing

The essence of this pattern is to set the types of values ​​through the restrictions imposed on them. And if possible, change this value so that it fits into these restrictions.

What is typing generally for? To catch the appearance of an error as early as possible, that is, as close as possible to the place where the programmer made an oversight, and not to where it caused an irreparable failure. But manually typing types is too expensive, so you need an automatic cast for compatible types. For example, “a string no longer than X containing only digits” can be uniquely converted to a “positive integer” and vice versa.

Many languages ​​do not allow you to define your own types. Some languages ​​allow it, but expressive means are often lacking. However, you can define a set of functions, each of which takes a value to the input and ensures that the value returned by it satisfies all the restrictions. At the same time, it can try to convert the value so that it meets the constraints, and if it fails, it should throw an exception.
')
We define several typical typekapters:
function aNumber( $val ){<br> if ( !is_numeric( $val ) ) throw new Exception( 'can not convert to number' );<br> return $val= +$val;<br>}<br><br> function aString( $val ){<br> return $val= $val . '' ;<br>}<br><br> function anArray( $val ){<br> if ( is_object( $val ) ) $val= get_object_vars( $val );<br> else if ( !is_array( $val ) ) throw new Exception( 'can not convert to array' );<br> return $val;<br>}

These are general purpose timbkasters. They make the casting of types anyhow; therefore, it is better to use more specific types that correspond to your business logic instead:
function anUserId( $val ){<br> aNumber( &$val );<br> if ( $val <= 0 ) throw new Exception( 'user id is not positive' );<br> return $val;<br>}

Notice that the typkasters first change the value of the parameter, and then return it. This allows you to use them in two forms:
function example( $count ){<br> aNumber( &$count ); // . <br> return aString( $count + 2 ); // . <br>}<br>$x= example( '2' ); // '4' <br>$y= example( '2x' ); //

Now a typical function that implements the adaptive typing pattern looks like this:
function xxx( $count= 0, $title= '' , $list= array(), $user= 0, $db= null ){<br> aNumber( &$count ); aString( &$title ); anArray( &$list ); anUserId( &$user ); DB::anInstance( $db );<br><br> var_dump( $count, $title, $list, $user );<br><br> return aString( $count * 2 );<br>}

However, it is possible that native support will be added to PCP soon and the recording will become more convenient:
function aString xxx( aNumber $count= 0, aString $title= '' , anArray $list= array(), anUserId $user= 0, DB $db= null ){<br> var_dump( $count, $title, $list, $user );<br><br> return $count * 2;<br>}

In fact, we then get a static typing of functions with automatic casting and the ability to independently determine new data types. In the meantime, unless you can use code generation or not be lazy ;-)

Source: https://habr.com/ru/post/95157/


All Articles