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

documented swift closures #2724

Open
wants to merge 1 commit into
base: feature/swift-bindings
Choose a base branch
from

Conversation

stephen-hawley
Copy link

Added docs that spell out how Swift closures are represented in memory and the calling conventions.


# Idiomatic Differences

The main idiomatic difference has to do with the escaping/non-escaping varieties of closure. Obviously, C# doesn't make this distinction. As such, if a C# method gets passed a delegate from Swift that is non-escaping, it incumbent upon the user to never store it. We can make this somewhat better by putting an attribute on such delegates that flags it as a non-escaping and create a Roslyn analyzer that looks for usage that would violate that.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe non-escaping closures can be modeled using a ref struct? Ref structs "are allocated on the stack and can't escape to the managed heap."

Calling the closure might end up a little bit weird in C# though, since I think you'd have to call a method on the struct instead of using delegate syntax.

ref struct MyNonEscapingClosure {
    public void Invoke (...)
    {
		// call the closure here
    }
}

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