5

I'm looking to try and cast a type based on a variable. The example below fails saying "Use of undeclared type, 'myType'"

protocol Parent {}
protocol Child: Parent {}

struct Foo: Child {}

let foo: Parent = Foo()

let myType = Child.self

if let _ = foo as? myType {
    print("success")
}

Is there any way to cast based on the variable myType?

1 Answer 1

1

Having reflected, casting is not the right question to ask here. Here i'm just checking for conformance. Dynamic casting would be useless as there's no way for the static analyzer to know what to do with it if it's casting based on a variable type.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.