kitlangton / neotype

A friendly newtype library for Scala 3
Apache License 2.0
169 stars 18 forks source link

Interaction with ZLayer #1

Closed soujiro32167 closed 8 months ago

soujiro32167 commented 1 year ago

related to https://github.com/zio/zio-prelude/issues/821

Alas, Izumi tag strikes again!

to reproduce:

import neotype.*
import zio.*

given NonEmptyString: Newtype[String] with
  inline def validate(input: String): Boolean =
    input.nonEmpty

val layer = ZLayer.succeed(NonEmptyString("foo"))

image

kitlangton commented 1 year ago

Just tested. I'll need a neotype-zio package for the integration. But it will work. I'll open something tonight!

kitlangton commented 1 year ago
  given [A, B](using newType: Newtype.WithType[A, B], tag: Tag[A]): Tag[B] =
    newType.unsafeWrapF(tag)

This makes it work.

soujiro32167 commented 1 year ago

My hero! This is the same technique as you use to derive other typeclasses (circe, quill) isn't it?

kitlangton commented 1 year ago

Update: There are still some issues. It can now derive the tag; however, there are tag materialization issues in some cases due to some widening that the ZLayer macros perform. I'll try and get everything fixed. It'll work for basic things atm.

kitlangton commented 8 months ago

After exploring some more recently, I think this needs to be fixed @ the izumi reflect level :(