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

Implement AnyView.init(_fromValue:) #20

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

nerdsupremacist
Copy link

@nerdsupremacist nerdsupremacist commented May 21, 2020

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
Copy link
Author

nerdsupremacist commented Jul 1, 2020

@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
Copy link
Author

@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
Copy link
Owner

Cosmo commented Oct 19, 2020

@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
Copy link
Author

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

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

Successfully merging this pull request may close these issues.

2 participants