Cosmo / OpenSwiftUI

WIP — OpenSwiftUI is an OpenSource implementation of Apple's SwiftUI DSL.
MIT License
1.41k stars 67 forks source link

Implement AnyView.init(_fromValue:) #20

Open nerdsupremacist opened 4 years ago

nerdsupremacist commented 4 years ago

This PR implements AnyView.init(_fromValue:).

I need this functionality for my library. Since I didn't know how to implement it, I first checked how you do it here, and noticed that you didn't. But it was marked as TODO.

I guess you probably found to be a real pain in the a**. I hope you don't hate my implementation. It uses some runtime functions, symbol lookups and exploits how protocol conformance looks during runtime. It's not exactly the simplest implementation. But I couldn't figure out a simpler way to do it.

I had to add a C target to be able to include _GNU_SOURCE under linux, because dlopen is not defined for Swift in Linux. Refer to this forum post for more info.

I have not tested this on linux, but it should work. I added two tests which you can run to check it.

nerdsupremacist commented 4 years ago

@Cosmo I just wanted to give credit to @a2 who figured out that the symbol lookup won't work in Linux and proposed using the extern keyword in C, since we don't really need to look up any symbol by its name. We only need the specific one.

This should now work easily on linux and embedded ;)

nerdsupremacist commented 4 years ago

@Cosmo I took the contents of this PR and managed to make it a bit more generic: https://github.com/nerdsupremacist/AssociatedTypeRequirementsKit.

I can switch it to that implementation if you feel like it, but I understand if you want to keep the number of dependencies of this as low as possible

Cosmo commented 4 years ago

@nerdsupremacist I finally took some time to take a look at the amazing work you did. AssociatedTypeRequirementsKit is super cool (== black magic).

To be honest, I won't mind having this as a dependency. It takes care of the heavy lifting of some issues that are close to impossible to solve for me -- and that took a big chunk of my motivation.

nerdsupremacist commented 4 years ago

Cool! I've changed the PR to only use the library instead ;)