Forum Discussion

rob_carr_76748's avatar
rob_carr_76748
Icon for Nimbostratus rankNimbostratus
Oct 02, 2018

iRule/TCL data type coercion

I'm working through some F5 training material for developing irules, and on the subject of how to make your rules more efficent, the following slide about data type coercion is presented:

 

 

The notes attached to this slide say:

 

The "incr x" command forces a data type conversion of the variable "x" into an integer. While "x" is still stored as a string, it is also likely that the iRule interpreter still has an integer version of this by the time it gets to the comparison, which occurs on the very next line. In this case, a polymorphic comparison with "==" would be faster than converting 4 into a string.

 

 

Rule of thumb – use "eq" or "equals" when you know both operands are strings, because it is faster. Use "==" if you are not sure.

 

 

When you access a variable, you can coerce it into a given data type depending on the operation you’re using it with. This means that the same variable can be interpreted differently in a series of different operations.

 

 

In this example, x is an integer, because incr only works with integers and so the value of 4 is coerced into being an integer.

 

 

If two strings are "large" and can be converted into integers, it’s generally faster to convert and compare rather than compare as long strings.

 

I'm having trouble making sense of this.

 

 

Looking at the left-hand example, 'x' starts out as a string, is coerced to an integer by the incr command, then compared to '4' (which is a string, not an integer as the first paragraph of the slide notes implies) via the equals operator, which only works on strings, requiring 'x' to be coerced back to being a string. Two coercion operations.

 

 

Looking at the right-hand example, 'x' starts out as a string, is coerced into an integer by the incr command, then compared to the string '4' via the polymorphic '==' operator, which handles all types of comparisons, but 'x' is now an integer and '4' is a string, so one of them is going to have to be coerced, either 'x' to a string or '4' to an integer, which brings us again to two coercion operations.

 

 

Clearly I'm not understanding something here.

 

1 Reply

  • Hi Rob,

    I'll probably get shot down by someone who knows TCL better than I do for trying to answer this but this is my interpretation.

    In the left hand example, "equals" is used as the operator. As this is not a polymorphic operator, it will need to convert a numerical 4 (fed from the left via the $x variable) back to a string in order to compare it.

    To break it down, for the operator

    $x equals 4
    to work, it would need to interpreted as
    string '4' equals string '4'
    (in order to return a Boolean true or false) - hence, $x which holds the value integer 4, needs to be converted, or coerced back into a string.

    With the example on the right-hand side,

    $x == 4
    can be interpreted as
    "integer 4 == integer 4"
    Which requires no coercion. This is a polymorphic operator so it can handle both strings and integers, so will attempts to use most appropriate format which in this case it an integer - conversion not necessary.

    TCL will always replace a variable with the literal value of that variable, so to visualise it, it can be helpful to write it as TCL will interpret it, then ask yourself, is that "4" a string or an integer, which operator do I need to feed that value into?

    The Operator is a machine that variables are fed into, we just need to make sure we feed them into the machine, best placed to process them.

    This link maybe helpful if you've not seen it already: https://devcentral.f5.com/articles/polymorphism-making-tcl-operators-work-for-you

    Lee