DEV Community

Peter + AI
Peter + AI

Posted on

Demystifying Uniface's $typed Function πŸš€

Hey developers! πŸ‘‹ If you're working with Rocket Uniface, you know that handling data types correctly is crucial. Today, let's dive into a super useful ProcScript function: $typed. It's a powerful tool for making sure your data is exactly the type you need it to be.

A little note: This post was drafted with the help of an AI to ensure clarity and provide solid examples based on the official Uniface 10.4 documentation.

What Exactly is $typed? πŸ€”

In simple terms, the $typed function lets you explicitly convert data from one type to another. Think of it as telling Uniface, "Hey, no matter what this data looks like, I want you to treat it as a String, a Number, or a Boolean."

The basic syntax looks like this:

$typed("DataTypeConverter(Value)")
Enter fullscreen mode Exit fullscreen mode

If you leave out the DataTypeConverter, Uniface assumes you want to convert the value to a string, like this:

$typed("Value") // This is the same as $typed("$string(Value)")
Enter fullscreen mode Exit fullscreen mode

Why is $typed So Important? ✨

You might be thinking, "But there are already functions like $number() and $date()!" And you're right. However, $typed is special because it's the only way to explicitly force a conversion to the following data types:

  • Boolean
  • Float
  • String (as a literal constant)

This is especially useful when you're building typed lists of parameters, for example, when using the activate/list command.

Let's See an Example πŸ’‘

The best way to understand $typed is to see it in action. Let's take the value "012345" and convert it into different data types.

variables
  string vList
endvariables

; 1. As a string
putitem vList, -1, $typed("$string(012345)")
; Result: "012345"

; 2. As a boolean (starts with '0', so it's False)
putitem vList, -1, $typed("$boolean(012345)")
; Result: "F"

; 3. As a boolean (starts with '1', so it's True)
putitem vList, -1, $typed("$boolean(12345)")
; Result: "T"

; 4. As a float
putitem vList, -1, $typed("$float(012345)")
; Result: 12345 (as a float)

; 5. As a number
putitem vList, -1, $number(012345)
; Result: 12345 (as a number)
Enter fullscreen mode Exit fullscreen mode

As you can see, the same initial value produces completely different results depending on the data type you specify. This control is what makes $typed so powerful.

A Common Pitfall: $typed("$string(...)") vs. $string(...)

Here's a quick tip to save you a headache! The $string converter used inside $typed is different from the standalone $string ProcScript function.

  • $string("A & B"): The standalone function is designed to convert XML entities. This would return "A & B". βœ…
  • $typed("$string(A & B)"): The converter inside $typed treats the input as a literal string. It does not convert the XML entity. This would return the original string "A & B". βœ…

It's a small but important difference to remember!

Conclusion

The $typed function is an essential tool in your Uniface toolkit for explicit data type conversion. It gives you precise control over your data, especially when you need to guarantee a value is treated as a Boolean, Float, or literal String.

Happy coding! πŸ’»

Top comments (0)