Tutorial: Type Casting

How to try and convert one type to another.

As previously described, we can sometimes try to cast a value this is incorrect, like the characters 'bob' into a number. In Quorum, there is a mechanism to detect when these errors happen. To do this, we have to inform Quorum that we want to 'check' for errors, and then what to do if we 'detect' one. Let's take our previous example one more time:

Converting one type to another

A common task in programming is to convert one kind of thing into another. For example, we might have typed some text, but need the computer to understand it as a number (or the opposite). Consider the following code snippet:

text someText = "4.2"
output someText

This text has the value of 4.2, but suppose we wanted the computer to use the actual numeric value, not the text. In Quorum, we do this using a special word named "cast." We can use casting by passing what kind of thing we would like to convert to and then the item we want to convert. Here is an example:

number value = cast(number, "4.2")
output value

While Quorum does its best to conduct casts for us, it sometimes prevents us from doing a conversion. For example, suppose we asked Quorum convert to 'bob' into an integer. The value of 'bob' is not a number, it is a name, so Quorum gives us an error while our programming is executing. This is called a cast error. Here is an example where we intentionally cause this problem:

number value = cast(number, "bob")
//This will throw a Casting Error when the program is executed.
output value

The important point here is that the above code is not a compiler error. The code compiles and runs correctly, but what we are asking Quorum to do, a conversion from the word 'bob' into a number, does not make sense and as such it is an error. Try some of these operations in the development environment below.

Casting Objects

It is also possible to cast object in Quorum. Suppose we were using the Math class from the Libraries.Compute package. This class computes various operations from math for us. It includes actions like rounding, sine, cosine, or other computations. In Quorum, our Math class is what we call an object. All this really means for the context of this tutorial is that we can create a copy of one and cast it. We do this like so:

use Libraries.Compute.Math
Math computer
Object o = cast(Object, computer)
Math bob = cast(Math, o)
output "I have cast an object back and forth from Math"

Casting is a complicated topic with objects and the example here , while intended to provide information on how to cast, does not really explain why we would want to. For this, we must understand inheritance, which will not be covered until later. For now, this is a reference.

Cast Errors

number value = cast(number, "bob")
//This will throw a Casting Error when the program is executed.
output value

If we surround our cast with check and detect, a mechanism that will be described in more detail in later tutorials, we can determine when an error occurs and take action. As an example, consider what happens when we output a value when the computer detects an incorrect cast:

check
   number value = cast(number, "bob")
    //This will throw a Casting Error when the program is executed.
   output value
detect e
   output "The cast failed, please enter new input."
end

There is more to handling errors in Quorum than outputting error messages to the screen (e.g., what is e? what are stack traces?). This tutorial has just scratched the surface.

Next Tutorial

In the next tutorial, we will discuss comments, which describes how to make notes and documentation to help us understand the code.