Skip to content

mtumilowicz/scala-zio2-partially-applied-types-workshop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status License: GPL v3

scala-zio2-partially-applied-types-workshop

preface

partially applied arguments

  • good analogy to set intuition on partially applied types
  • example
    • problem
      def map[A, B](list: List[A], f: A => B): List[B] =
        list.map(f)
      
      val nums = List(1,2,3)
      
      mapping function(nums, _ + 1) // not compiling
      mapping function(nums, (i: Int) => i + 1) // compiling
      
    • solution - partial application of arguments
      def map[A, B](list: List[A])(f: A => B): List[B] =
        list.map(f)
      
      val nums = List(1,2,3)
      
      mapping function(nums)(_ + 1)
      

partially applied types

  • in scala, you can specify either all types or none params
  • example
    • problem
      def serviceWithZIO[Service: Tag, R, E, A](f: Service => ZIO[R, E, A]): ZIO[R with Service, E, A] =
        ZIO.service[Service].flatMap(service => f(service))
      
      • Service and R, E, A are not connected - Service has to be specified, rest can be inferred
    • solution - partial application of types
      def serviceWithZIO2[Service] =
        new ServiceWithZIOPartiallyApplied[Service]
      
      final class ServiceWithZIOPartiallyApplied[Service]() {
        def apply[R, E, A](f: Service => ZIO[R, E, A])(implicit tag: Tag[Service]): ZIO[R with Service, E, A] =
          ZIO.service[Service].flatMap(service => f(service)) // tag is needed by ZIO.service[Service]
      }
      
  • usually used for wrappers, where we have groups of unconnected types
  • performance
    • to keep it at zero cost, we have to use 'AnyVal'
      final class ServiceWithZIOPartiallyApplied[Service](private val dummy: Boolean = true) extends AnyVal {
          ...
      }
      

Releases

No releases published

Packages

No packages published

Languages