Type Checking and Casting
The is operator checks whether an expression's runtime type is the specified type (or a subtype), returning a boolean value. The as operator attempts to convert an expression to the specified type, returning Some(value) on success and None on failure.
Variables of interface type can only access interface methods. If you need to access type-specific members, you must first check the type with is, then convert with as. This is typically used with match expressions to handle conversion results.
Type conversion is safe: as returns an Option type that forces handling of failure cases, avoiding runtime exceptions. This differs from forced type casting in some languages that throw exceptions on conversion failure.
In this example, variables of type Animal might actually be Dog or Cat. After using is to check the type, we use as for safe conversion and access to type-specific methods.
Modify the code to create an Animal object (instead of Dog) and observe that the as Dog conversion returns None.
is and as in your code, it may indicate that the interface design needs improvement.