GithubHelp home page GithubHelp logo

Comments (4)

AliSoftware avatar AliSoftware commented on July 20, 2024

Hello

I see two potential reasons for the error you see

  1. You pass viewType: headerType but headerType is a function returning your type, and you didn't call it. Didn't you want to use viewType: headerType() instead, to call the function so that it returns the expected (UICollectionReusableView & Reusable).Type?

  2. Sadly I'm not sure if the current version of Swift is capable of going to such levels of abstraction just yet. Tbh I'm actually quite surprised that (UICollectionReusableView & Reusable).Type would compile (meaning that Swift would somehow reference a metatype built dynamically from two protocols? reminds me of similar limitations of current version of Swift like not being able to make a meta type conform to a protocol, and other limitations like the ones forcing us to use type erasure, etc)

I think if you make your headerType() function's return type be concrete type instead of (UICollectionReusableView & Reusable).Type it would work. e.g. if you have class CommonRV: UICollectionReusableView, Reusable {} then use class MyRV1: CommonRV & class MyRV2: CommonRV in your code, you could have func headerType() -> CommonRV.Type { /* return MyRV1.Type or MyRV2.Type depending on some condition */ } and I think in that case it would work.

tl;dr I think what make Swift compiler unhappy here is that (UICollectionReusableView & Reusable).Type is maybe a little too complex for it to grasp, being a meta-type of a combination of a class and a protocol, not sure it's able to handle that just yet.

If that test confirms my hypothesis nĀ°2, then your solution is either to do what I described above (make all your concrete classes of UICollectionReusableView that you want to use with Reusable be a subclass of class CommonRV: UICollectionReusableView, Reusable and not subclasses of UICollectionReusableView directly), or if you don't want to use inheritance, maybe we could find a workaround solution using type erasure? I'm not sure

from reusable.

amaurydavid avatar amaurydavid commented on July 20, 2024

The first potential reason you point is in fact a typo. In my example, the var and the func have the same name, but it's not the case in my code. I'll update my post to better reflect that.

About your second potential reason, I've tried to simplify like that:

typealias MyHeader = UICollectionReusableView & Reusable

//This func is responsible for returning the correct header class according to the given indexPath
func retrieveHeaderType() -> MyHeader.Type { ... }

But I've got the same issue as the typealias doesn't change anything from the compiler pov.

I've also thought about having all my headers inherit from MyHeader but I wonder if it will correctly dequeue a MyHeader or its subclass... I'll try.

from reusable.

amaurydavid avatar amaurydavid commented on July 20, 2024

if you have class CommonRV: UICollectionReusableView, Reusable {} then use class MyRV1: CommonRV & class MyRV2: CommonRV in your code, you could have func headerType() -> CommonRV.Type { /* return MyRV1.Type or MyRV2.Type depending on some condition */ } and I think in that case it would work.

It indeed compiles and works as expected :). Thanks!

from reusable.

AliSoftware avatar AliSoftware commented on July 20, 2024

Thanks for the follow up!
(And I hope a future version of Swift will allow such composed metatypes construct!)

from reusable.

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.