Forum Discussion
iRule/TCL data type coercion
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
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com