Giter VIP home page Giter VIP logo

Comments (3)

ra1028 avatar ra1028 commented on May 27, 2024

@ruixingchen
Swift Array is not allowed to use existential type that have associated type to Element.
You can extends the Differentiable protocol by wrapping it with struct or enum.
e,g.

struct EntityWrapper: Differentiable {
    var base: Entity
    var id: String    { base.id }

    ...
}

var dataSource: [EntityWrapper] = []
struct A: Entity {...}
struct B: Entity {...}
struct C: Entity {...}

enum EntityVariant: Differentiable {
    case a(A)
    case b(B)
    case c(C)

   ...
}

var dataSource: [EntityVariant] = []

from differencekit.

ruixingchen avatar ruixingchen commented on May 27, 2024

@ra1028
thanks, but sadly not working ๐Ÿ˜ข

struct EntityWrapper: Differentiable {
    var base: Entity //ERROR HERE
    var id: String    { base.id }
    ...
}

Xcode says that Protocol 'Entity' can only be used as a generic constraint because it has Self or associated type requirements

I used associatedType in one of my protocol, but then the protocol comes to be nothing, it can only be used as a generic constraint, I cant use it as a TYPE, or put it in an array, at last I remove the associatedType and use Any instead, now the protocol is free and wild ๐Ÿ˜œ

from differencekit.

ra1028 avatar ra1028 commented on May 27, 2024

@ruixingchen
I looked at your sample code and suggested to implement it with wrapper type because Entity can be used as an existential type if the Differentiable conformance is removed from it.
If the Entity has an associated type for other reasons, I recommend to use the enum that I suggested before.
The further discussion will be about Swift's existential type, and the issue will deviate from "how to use DifferenceKit".

from differencekit.

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.