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

casting and unwrapping at the same time #1386

Closed
bjartek opened this issue Jan 27, 2022 · 1 comment
Closed

casting and unwrapping at the same time #1386

bjartek opened this issue Jan 27, 2022 · 1 comment

Comments

@bjartek
Copy link
Contributor

bjartek commented Jan 27, 2022

In the curent metadata standard there is a method
fun resolveView(_ type:Type) : AnyStruct?

When you call that method you very often want to cast the resulting object as the type you sent in. So something like this?

let metadata=collection.borrowViewResolver(id: id).resolveView(Type<Voucher.Metadata>()) as! Voucher.Metadata

This gives no compile error as you have use as! but it will never succeed. The reason is that since resolveView returns an option you first have to unrwap it and then cast it. So this code will work

let metadata=collection.borrowViewResolver(id: id).resolveView(Type<Voucher.Metadata>())! as! Voucher.Metadata

Another issue I am pondering is why we need as! here. If i remove it i get
mismatched types. expected Vouchre.Metadata?, got AnyStruct? but I have clearly ! the value before i cast it.

@turbolent
Copy link
Member

I think with #3580 this should be supported now.

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
@bjartek @turbolent and others