Can someone help me understand what this associated type constraint actually says

Found this piece of code somewhere on the net:

public protocol Functor {

/// (* -> *)

associatedtype FA: Functor = Self // HERE: - is it perhaps the same/shortcut for 'where FA == Self' ?

/// *

associatedtype A

/// fmap

func fmap(_ f: @escaping ((A) -> B)) -> FA where FA.A == B


Thanks, tried to locate it googling for the docs but failed miserably.


1 Like

It's a default for FA. When an implementation of the protocol doesn't declare FA - Self will be used.

struct F1: Functor {}
struct F2: Functor {}
struct F3: Functor {
  typealias FA = F2

F1.FA == F1 // FA is defaulted to `Self` which is `F1` here
F2.FA == F2 // FA is defaulted to `Self` which is `F2` here
F3.FA == F2 // FA is specified by the implementation, so it's `F2`

Yes, that's what I was looking for, thanks!