Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to tagless final API #14

Closed
chuwy opened this issue Feb 3, 2019 · 1 comment
Closed

Switch to tagless final API #14

chuwy opened this issue Feb 3, 2019 · 1 comment
Milestone

Comments

@chuwy
Copy link
Contributor

chuwy commented Feb 3, 2019

Since we need abstract over effects in all our libraries IO-like for general purpose and strict Id/Either for Beam, we cannot have Sync in type signatures anymore and hence generic F[_] is our only option.

Applications such as Iglu Resolver or Scala Weather will need to require its their own Cache typeclass:

class Resolver[F[_]] {
  def resolve(key: SchemaKey)(implicit C: Cache[F, SchemaKey, LookupResult]): F[Either[Failure, Schema]]
}

In lru-map codebase this can be implemented as following:

trait Cache[F[_], K, V] {
  def get(key: K): F[Option[V]]
  def put(key: K, value: V): F[Unit]
}

object Cache {
  def mutableCache[K, V](size: Int): Cache[Id, K, V] = new Cache[Id, K, V] {
    private val underlying = new MutableCache[K, V](new ImpureLruMap[K, V](size, 16, 0.75f))
    def get(key: K): Id[Option[V]] = underlying.get(key)
    def put(key: K, value: V): Id[Unit] = underlying.put(key, value)
  }

  def ioCache[F[_]: Sync, K, V](size: Int): Cache[F, K, V] = new Cache[F, K, V] { self =>
    def get(key: K): F[Option[V]] = ???
    def put(key: K, value: V): F[Unit] = ???
  }
}

The biggest downside is that we'll have to make above interpreters implicit by ourselves after size is known.

Example (gives different results for 0 and non-0 sizes):

  def test[C[_]](implicit C: Cache[C, String, Int], M: Monad[C]): C[Int] =
    for {
      _ <- C.put("foo", 3)
      f <- C.get("foo")
      result = for { ff <- f; fff <- f } yield ff * fff
      _ <- C.put("bar", result.getOrElse(0))
      r <- C.get("bar")
    } yield r.getOrElse(1)
@chuwy chuwy changed the title Introduce tagless encoding Get rid of Sync context Feb 4, 2019
chuwy added a commit that referenced this issue Feb 4, 2019
@chuwy chuwy mentioned this issue Feb 4, 2019
6 tasks
@chuwy chuwy changed the title Get rid of Sync context Switch to final tagless API Feb 8, 2019
@chuwy
Copy link
Contributor Author

chuwy commented Feb 8, 2019

Renamed the ticket to be consistent with other client-libraries snowplow/iglu-scala-client#113

@chuwy chuwy changed the title Switch to final tagless API Switch to tagless final API Feb 8, 2019
@chuwy chuwy mentioned this issue Mar 4, 2019
@chuwy chuwy added this to the Version 0.3.0 milestone Apr 10, 2019
chuwy added a commit that referenced this issue Apr 10, 2019
@chuwy chuwy closed this as completed in c97bbd4 Jul 25, 2019
chuwy added a commit that referenced this issue Jun 9, 2020
chuwy added a commit that referenced this issue Jun 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant