📜 ⬆️ ⬇️

Perl6 - Working with Data Types

Until now, articles only had a superficial mention of data types — declaring a variable of a certain type, or indicating the result of an operation, but all we could do was endure the compiler’s antics — “I want to die, I want to change the type, or just change the type ".
Actually, for those who want to feel like a god at least somehow important in the management of types in their own script, welcome under cat.


So, let's say we wrote a function that takes two variables as parameters:
sub Func($a, $b) {...} 


And we need the function to always work in one way, but if suddenly a fractional number and a string, or vice versa, are passed as parameters, then perform other actions.
')
Let's start small - find out what kind of data we have in each of the variables, for this we use the WHAT method, which returns a type object - an object indicating the data type (for example, the value "10" will return the object "(Int)"). It should be noted that it is the object that is returned, not the string, and a simple comparison of the two types through, for example, == or eq will not work. In order to compare the resulting object with the type, we use the smart comparison operator '~~':
 sub Func($a, $b) { $condition = ( ($a.WHAT ~~ (Rat)) ?& ($b.WHAT ~~ (Str)) ); $condition ?|= ( ($a.WHAT ~~ (Str)) ?& ($b.WHAT ~~ (Rat)) ); if ($condition) { #`(SomeWork) } else { #`{AnotherWork} } } 

(Rat) and (Str) are type objects of real numbers and strings, respectively.
For those who have already forgotten? & - Logical "And"? | - Logical "OR".

And so, we learned to determine the type of value of a variable, and depending on it, to perform any actions. But let's see in which cases it is necessary to do this:
 my Int $a; sub Func($p) { $a = $p; } Func(10); Func("Test"); 

In our case, the script dies when it tries to assign a string to a variable that can contain only integers.

Let's adjust our example a little, and write the following:
  $a = +($p); 

As a result, we also say an error, and the script will die, and the error will not be about assignment but about the wrong type in a numeric context, from which we conclude that private contexts only add type constraints, and that for manual type conversion we need something different.

Let's try to use the following construction:
  $a = Int($p); 

Already better, because if we pass a number of type (Rat), then it will be converted to type (Int), but if we pass a string, we will get the error of converting a string to a number, namely that the string should look like numbers in the 10th number system.
So far I have found only one way to make the script work further - catching exceptions. In my opinion, this is the most logical action, but we'll talk about this in the next articles.

Also, our script may die if the function tries to return a value inconsistent with the description:
 sub Func($p) of Int { return $p; } Func("test"); 

So in this case, it would also do the right thing to check the output values.

Lastly, I want to say that, in my opinion, restrictions on the types of stored data can greatly help in describing the "interfaces" of functions, and provide some advantages to typed programming languages.
Hope this info was helpful.

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


All Articles