icerockdev / moko-kswift

Swift-friendly api generator for Kotlin/Native frameworks
https://moko.icerock.dev
Apache License 2.0
351 stars 21 forks source link

#46 Remove bang operator to eliminate Xcode warnings #47

Closed atticus183 closed 2 years ago

atticus183 commented 2 years ago

This PR was created to eliminate the following Xcode warning:

Forced cast from 'Class.dataclass1' to 'Class.dataclass2' always succeeds

Update

It looks like my proposed fix of replacing as! with as breaks the sample app 😞. My apologies. This replacement seemed to resolve the issue with the project I'm on, but it may not be the way forward for everyone.

Alex009 commented 2 years ago

so, as we see this forcecast required, but maybe we can generate some comment for Xcode to ignore this warning? just like in Kotlin we have @Suppress

Alex009 commented 2 years ago

https://developer.apple.com/forums/thread/18911?answerId=59439022#59439022 this answer maybe help

nateansel commented 2 years ago

Hi @Alex009! I'm also working on the project with @atticus183 where we are running into the issue described in #46. We have looked into lots of ways to get around the warnings that Xcode is showing us and we weren't able to find a solution that we liked. The main issue is that both #pragma commands and compiler flags (for specific files) are not supported by the Swift compiler, so we aren't able to ignore the warnings in the moko-kswift generated file.

With that in mind I took a look at the sealed class to Swift enum feature in kswift to see if I could come up with a solution that would allow generics to be force-cast when needed while not utilizing a force-cast when it is not necessary. I think I found a solution that I am happy with, but I am curious to hear what you think!

I added additional logic to the .sealed property generation that uses a regex pattern to perform some additional checks to determine if a force-cast is necessary. The logic I added amounts to the following rules:

I also added some additional tests for both non-generic and generic sealed classes to make sure that the logic I added works correctly.