Verse Library verse

06 Converting An Object Reference To A Different Type

Demonstrates casting objects to subclasses and implemented interfaces with safe type checking.

extracted-snippets/documentation/en-us/uefn/type-casting-and-conversion-in-verse/06-converting-an-object-reference-to-a-different-type.verse

# Source URL: https://dev.epicgames.com/documentation/en-us/uefn/type-casting-and-conversion-in-verse
# Local doc:  epic-docs/documentation/en-us/uefn/type-casting-and-conversion-in-verse.md
# Section:    Converting an Object Reference to a Different Type
# Class and interface definitions
positionable := interface() {}
shape := class<abstract>(positionable) {}
triangle := class(shape) {}
square := class(shape) {}

# Create a square object referenced using the superclass type shape
MyShape:shape = square{}

# This will succeed since MySquare is a square object
if(MySquare := square[MyShape]):
    Print("Successfully cast shape to square")

if(MyTriangle := triangle[MyShape]):
    Print("This will never print.")
else:
    Print("Failed to cast MyShape to triangle. This is expected behavior.")

# This will succeed since the positionable interface must be implemented by subclasses of shape
if(MyDrawable := positionable[MyShape]):
    Print("Successfully cast shape to positionable")
Copy full snippet

Comments

    Sign in to vote, comment, or suggest an edit. Sign in