Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic casting should unwrap optional values #3183

Open
austinkline opened this issue Mar 20, 2024 · 2 comments
Open

Dynamic casting should unwrap optional values #3183

austinkline opened this issue Mar 20, 2024 · 2 comments

Comments

@austinkline
Copy link

austinkline commented Mar 20, 2024

Current Behavior

Dynamic casting (failable casting as?, and force casting as!) an optional value (potentially type-erased to an existential) to a non-optional type currently fails (e.g. failable casting results in nil).

Expected Behavior

I expected (but perhaps shouldn't have) that an optional value being casted into a subtype it conforms to would unwrap it. I'm really not sure if this is a bug or if it's expected though

Steps To Reproduce

I've made a repro contract/transactions to demonstrate

https://github.com/austinkline/cadence-burner-bug

Environment

- Cadence version:Version: v1.15.0-cadence-v1.0.0-preview.12, Commit: c582e63baf4581e4cf021a9b1917c198ca5b6065
- Network: emulator
@austinkline austinkline added Bug Something isn't working Feedback labels Mar 20, 2024
@j1010001
Copy link
Member

@turbolent turbolent added Feature and removed Bug Something isn't working labels Mar 26, 2024
@turbolent turbolent changed the title if let casting does not fully unwrap optional values Failable casting does not unwrap optional values Mar 27, 2024
@turbolent turbolent changed the title Failable casting does not unwrap optional values Failable casting should unwrap optional values Mar 28, 2024
@turbolent turbolent changed the title Failable casting should unwrap optional values Dynamic casting should unwrap optional values Mar 28, 2024
@turbolent
Copy link
Member

turbolent commented Mar 28, 2024

I guess that makes sense. For example, Swift also supports unwrapping using casting:

let hello: String??? = "hello"
let something: Any = hello 
something as! String   // "hello" of type `String`

However, casting to Any does not:

something as! Any   // Optional(Optional(Optional("hello"))) of type `String???`

Then again, the behaviour does not seem to be about existential types, because those are also unwrapped:

protocol I {}

struct S: I {}

let hello: S???? = S()
let something: Any = hello 
something as! any I   // S() of type `S`, not `Optional(Optional(Optional(S())))` of type `S???`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants