scalatagless-final

Higher kinded type and Tagless final


I'm trying to write a function that can take any tagless final trait and return F[String].

def apply[Api[F[_]]](implementation: Api[F[_]]): F[String] = ???

I don't understand why the above is not compiling.

The following works.

trait Api[F[_]]

def apply[F[_]](implementation: Api[F[_]]): F[String] = ???

But how can I get rid of this trait?


Solution

  • Try

    def apply[Api[_[_]], F[_]](implementation: Api[F]): F[String] = ???
    

    When you write apply[Api[F[_]]](..) you don't declare Api and F, you declare only Api. There F doesn't matter, you can write apply[Api[F[_]]](..) or apply[Api[G[_]]](..) or just apply[Api[_[_]]](..), anyway you can't use F outside.

    Api[F[_]] is correct syntax in declaration of type parameter, in type application in type position you should write Api[F].