Code Monkey home page Code Monkey logo

Comments (4)

jkotas avatar jkotas commented on August 21, 2024 1

Associated type doesn't have direct mapping in C# and could be represented with generics:

The prevalent opinion in previous discussions has been that mapping of protocols and associated types to C# is going to be hard. Where is the catch?

Nit:

public struct Never
{
	private Never() {}
}

This fails to compile with "The parameterless struct constructor must be 'public'.".

from runtimelab.

kotlarmilos avatar kotlarmilos commented on August 21, 2024

This issue has been updated with a better-defined goal -- feel free to provide feedback on the general direction or identified projections.

from runtimelab.

kotlarmilos avatar kotlarmilos commented on August 21, 2024

The prevalent opinion in previous discussions has been that mapping of protocols and associated types to C# is going to be hard. Where is the catch?

My understanding is that some scenarios would be harder to implement than others. Let's start with simple scenarios first.

Generic methods are used in the CryptoKit and SwiftUI dev templates. For these, we need to pass an implicit metadata pointer for the concrete type that can be located and exposed as PInvoke during binding process.

Generic methods with protocol constraints require an implicit protocol witness table parameter for conformance. There are two ways to retrieve them: statically and dynamically. Protocol conformance records are emitted in __TEXT.__swift5_proto binary section and each record contains a pointer to protocol descriptor and protocol witness table. One option is to scan it and map them to a hash table during the binding process. Alternatively, they can be retrieved at runtime using swift_conformsToProtocol. Here is an interesting article on performance of protocol conformances scanning during app startup: https://medium.com/geekculture/the-surprising-cost-of-protocol-conformances-in-swift-dfa5db15ac0c.

Methods with protocols used as types - instead of passing C# interface pointers within the projection tooling, we need to pass a boxed existential container. We need to implement their construction within the projection tooling, probably at runtime, which will depend on protocol witness tables retrieval.

An addition to above-mentioned scenarios could be types with generics, which require passing concrete type metadata and calling swift_instantiateConcreteTypeFromMangledName to create a concrete type.

For protocols with associated types, I haven't yet encountered differences in public API and underlying calling convention. I may be missing something here. This is a brief overview of these scenarios, and I will initiate the design on protocols while keeping this issue open for further requirements collection.

from runtimelab.

jkotas avatar jkotas commented on August 21, 2024

I agree that we should be always able to setup the right arguments and call the method. I am more worried about the projected API surface that we expose to user when protocols and generics are in the mix. Is the C# code that people end up writing against the projected API surface going to look reasonable?

from runtimelab.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.