Search code examples
swiftinheritancef#uicollectionviewsuperclass

Inheriting Multiple Classes in F#


I have the following class definition in Swift:

class LandlordHome : UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

}

I'm trying to create it in F#, however the inheritance pattern in F# only allows me to inherit one class like this:

[<Register ("LandlordHome")>]
type LandlordHome (handle:IntPtr) =
inherit UIViewController (handle)

If I try to add additional classes like this:

[<Register ("LandlordHome")>]
type LandlordHome (handle:IntPtr) =
inherit UIViewController, UICollectionViewDelegate, UICollectionViewDataSource (handle)

this throws an error.


Solution

  • UICollectionViewDelegate and UICollectionViewDataSource are not classes, but protocols, which are like interfaces in F#

    So your could should look something like this (abstract code):

    [<Register ("LandlordHome")>]
    type LandlordHome (handle:IntPtr) =
        inherit UIViewController (handle)
        interface UICollectionViewDelegate with
           // implementation of UICollectionViewDelegate
        interface UICollectionViewDataSource with
           // implementation of UICollectionViewDataSource