Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    Cary Robbins
    @carymrobbins
    :tada:
    Cary Robbins
    @carymrobbins
    @bhgitgit put the triple backticks on a single line by themselves :wink:
    image.png
    This message was deleted
    also, could you paste the error message?
    Ryan Mehri
    @rmehri01
    Hello, sorry this is probably asked a lot but I'm working through pfp in scala and I noticed that recently the readme was changed to not recommend the coercible instance trick but I find that explicitly defining the codec instances is somewhat clunky, so is there an alternative easier way to define these codecs and what are the problems with the trick besides increased compile times. Thanks!
    Cary Robbins
    @carymrobbins
    @rmehri01 I should add to the readme that you can use scalaz-deriving to get a less boilerplate-y way to derive newtype instances
    @deriving(Encoder, Decoder)
    @newtype case class Bar(s: String)
    Ryan Mehri
    @rmehri01
    Ah okay, that looks pretty good. Thanks!
    Thomas G.
    @ThomasGrt

    Hi and first of all thanks for the lib!

    Is it possible to have the newtype instances derivation outside of the newtype companion object?
    I ask this because I'm building a lib and I would like to let the clients of the lib (other processes) add the type class they need in their code.

    I saw something like this on the project readme

    object EqOrphans {
      implicit val eqFoo: Eq[Foo] = implicitly[Eq[Int]].coerce
    }

    but when I try it gives me an error :
    Error:(14, 53) could not find implicit value for parameter ev: io.estatico.newtype.Coercible[cats.Eq[Int],B]
    implicit val eqFoo: Eq[Foo] = implicitly[Eq[Int]].coerce

    Thanks in advance for your help

    Cary Robbins
    @carymrobbins
    @ThomasGrt Same implicit rules should apply. Make sure your orphan instance is in the implicit search scope. This may mean having to explicitly import it, e.g. import EqOrphans.eqFoo or import EqOrphans._
    Thomas G.
    @ThomasGrt

    Thanks for your quick answer!
    Sorry I must be doing something wrong but I cannot see what...

    This compiles :

    object NewTypeEq extends App {
      import cats.Eq
      import io.estatico.newtype.macros.newtype
      import io.estatico.newtype.ops._
      import cats._, cats.implicits._
    
      @newtype case class Foo(x: Int)
      object Foo {
        implicit val eqFoo: Eq[Foo] = implicitly[Eq[Int]].coerce
      }
    
      println(Foo(1) === Foo(2))
    }

    this does not compile :

    object NewTypeEq2 extends App {
      import cats.Eq
      import io.estatico.newtype.macros.newtype
      import io.estatico.newtype.ops._
      import cats._, cats.implicits._
    
      @newtype case class Foo(x: Int)
    
      object EqOrphans {
        implicit val eqFoo: Eq[Foo] = implicitly[Eq[Int]].coerce
      }
    
      import EqOrphans._
      println(Foo(1) === Foo(2))
    }

    Error is on line

     implicit val eqFoo: Eq[Foo] = implicitly[Eq[Int]].coerce

    could not find implicit value for parameter ev: io.estatico.newtype.Coercible[cats.Eq[Int],B]

    Thomas G.
    @ThomasGrt
    looks like it works with :
    object EqOrphans {
        import Foo._
        implicit val eqFoo: Eq[Foo] = implicitly[Eq[Int]].coerce
      }
    Łukasz Drygała
    @ldrygala

    Hi
    I'm trying to use a newtype instead of AnyVal in my code. I have a problem with slick column mappers:

    @newtype case class MyId(id: Long)
    
    implicit lazy val myIdMapper: JdbcType[MyId] =
      MappedColumnType.base[MyId, Long](
        _.id,
        MyId.apply
      )

    it fails with:
    No ClassTag available for MyId
    [error] MappedColumnType.base[MyId, Long](

    is there any way to generate ClassTag?

    Cary Robbins
    @carymrobbins

    @ldrygala Hmm may need to just derive it. Maybe try -

    import io.estatico.newtype.ops._
    
    implicit lazy val myIdMapper: JdbcType[MyId] = implicitly[JdbcType[Long]].coerce[JdbcType[MyId]]

    Not sure if lazy is actually needed here. Also not sure if this breaks some invariants about slick.

    If you can, you can derive this in the newtype companion
    @newtype case class MyId(id: Long)
    object MyId {
      implicit lazy val jdbcType: JdbcType[MyId] = deriving
    }
    I don't think generating a ClassTag is correct since MyId doesn't really correspond to a Class
    Łukasz Drygała
    @ldrygala
    thanks @carymrobbins for help
    Cary Robbins
    @carymrobbins
    @ldrygala For sure, did that work out for you?
    Jonas Hoffmann
    @jhoffmann87_gitlab
    Hi, I'm wondering how newtype compares to Scala's value classes. Why should I use newtype over value classes? When should I avoid using newtype?
    Dmytro Mitin
    @DmytroMitin
    @jhoffmann87_gitlab value classes have many limitations https://docs.scala-lang.org/overviews/core/value-classes.html
    Gabriel Volpe
    @gvolpe

    Hey @carymrobbins wondering if you have any clue why this would happen? (cross-posting from the Shapeless room)

    This compiles:

    sealed trait Yay
    object Yay {
      @newtype case class MyId(value: java.util.UUID)
    
      case class Foo(x: Int) extends Yay
      case class Bar(y: Yay.MyId, z: String) extends Yay
    }
    
    Check[Yay]

    This doesn't:

    sealed trait Yay
    object Yay {
      @newtype case class MyId(value: java.util.UUID)
    
      case class Foo(x: Int) extends Yay
      case class Bar(y: MyId, z: String) extends Yay
    }
    
    Check[Yay]

    The only difference is the type that Bar takes, MyId vs Yay.MyId. One compiles, the other doesn't. The implementation of Check relies on some Shapeless stuff, here's a Scastie that quickly demonstrates the issue: https://scastie.scala-lang.org/6TZGnRWIRDCBQxYDPOYreA

    Could it be macro expansion kicks in after Shapeless processes the datatypes?

    Cary Robbins
    @carymrobbins

    @gvolpe I'm not sure, but I'm betting that the bug is somewhere in either the implementation of Check or the shapeless side of things.

    You can use @newtype(debug=true) case class MyId(value: java.util.UUID) to have the compiler spit out the generated Scala code. By replacing the macro invocation with the generated Scala, we can rule out macro expansion.

    sealed trait Yay
    object Yay {
      //@newtype(debug=true) case class MyId(value: java.util.UUID)
      type MyId = MyId.Type
      object MyId {
        type Repr = java.util.UUID
        type Base = { type __MyId__newtype }
        trait Tag
        type Type <: Base with Tag
      }
    
      case class Foo(x: Int) extends Yay
      case class Bar(y: MyId, z: String) extends Yay
    }
    
    Check[Yay]
    could not find implicit value for parameter ev: Playground.Check.Checks[Playground.Yay]

    However, if we use

      case class Bar(y: Yay.MyId, z: String) extends Yay

    as you did before, it compiles.

    Gabriel Volpe
    @gvolpe
    Thanks for getting back to me, @carymrobbins. I got a reply in the shapeless room and we minimized the example even further. It's unrelated to newtypes, sorry for the trouble.
    Cary Robbins
    @carymrobbins
    @gvolpe No problem at all, glad you got it worked out.
    Eric Meisel
    @etspaceman

    Hey there. I'm having a hard time converting my library's AnyVal types to use this library and was hoping to get some help.

    Here is an example of a class that I'm attempting to convert: https://github.com/etspaceman/scalacheck-faker/blob/master/src/main/scala/faker/address/BuildingNumber.scala

    My code after the conversion is:

    package faker.address
    
    import org.scalacheck.{Arbitrary, Gen}
    import faker.ResourceLoader
    import faker.syntax.string._
    import io.estatico.newtype.macros.newtype
    
    @newtype final case class BuildingNumber private(value: String)
    
    object BuildingNumber {
      implicit def buildingNumberArbitrary(implicit
          loader: ResourceLoader
      ): Arbitrary[BuildingNumber] =
        Arbitrary(
          Gen
            .sequence[Seq[String], String](
              loader
                .loadKey[Seq[String]]("address.building-numbers")
                .map(_.interpolatedGen)
            )
            .flatMap(x => Gen.oneOf(x).map(apply))
        )
    }

    But I get an error:

    [error] C:\Users\erics\Documents\GitHub\scalacheck-faker\src\main\scala\faker\address\BuildingNumber.scala:8:27: macro annotation could not be expanded (you cannot use a macro annotation in the same compilation run that defines it)
    [error] @newtype final case class BuildingNumber private (value: String)
    [error]

    Pretty confused on what's going on here. Hoping to get some help

    I also tried replacing Gen.oneOf(x).map(apply) with Gen.oneOf(x).coerce but was getting a Coerceable not found error
    Another thing I thought of was to remove uses of the BuildingNumber class from the types itself, so I created a separate module for my types and for the classes / tests that referenced it, but received the same errors
    @carymrobbins I'm sure I'm doing something wrong. Mind guiding me here?
    Cary Robbins
    @carymrobbins
    unfortunately, you can't define a newtype at the top level
    it needs to be nested under an object of some kind
    a package object suffices
    Eric Meisel
    @etspaceman
    Got it. Thanks
    Igal Tabachnik
    @hmemcpy
    Good evening! I was wondering, why couldn't the macro emit a TypeTag that would just summon the Repr's type tag? I guess that's what you'd want to do anyway, right?
    Cary Robbins
    @carymrobbins
    @hmemcpy I think that might not be exactly what we'd want. Ideally, a TypeTag would contain the type information of the type in question, so we'd lose type info that way. If we chose to do what you are saying, it might be preferable to allow people to opt in to it (as a macro parameter or something) that way if we find a better way to do it in the future that doesn't lose type information we won't be stuck with the hack.
    Nikita
    @joomalungma
    This message was deleted
    This message was deleted
    Nikita
    @joomalungma

    Hey there. Trying to getting work circe and new type example from Practical FP in scala and have no luck with newtype v0.4.4. Can't find correct imports. For this

    import io.circe._
    import io.circe.generic.semiauto._
    import io.circe.parser._
    import io.circe.syntax._
    import io.estatico.newtype.Coercible
    import io.estatico.newtype.macros.newtype
    import io.estatico.newtype.ops._
    trait CoercibleCodecs {
         implicit def coercibleDecoder[A: Coercible[B, *], B: Decoder]: Decoder[A] =
                Decoder[B].map(_.coerce[A])
            implicit def coercibleEncoder[A: Coercible[B, *], B: Encoder]: Encoder[A] =
                Encoder[B].contramap(_.repr.asInstanceOf[B])
            implicit def coercibleKeyDecoder[A: Coercible[B, *], B: KeyDecoder]: KeyDecoder[A] =
                KeyDecoder[B].map(_.coerce[A])
            implicit def coercibleKeyEncoder[A: Coercible[B, *], B: KeyEncoder]: KeyEncoder[A] =
                KeyEncoder[B].contramap[A](_.repr.asInstanceOf[B])
        }

    getting error like
    value repr is not a member of type parameter A [error] KeyEncoder[B].contramap[A](_.repr.asInstanceOf[B])
    What is the correct import?

    Cary Robbins
    @carymrobbins
    @joomalungma It's not recommended to write instances in that way, see - https://github.com/estatico/scala-newtype#coercible-instance-trick
    For your specific type error, I assume it is asking for a function of A => B. There's no reason to think that A could have the field .repr. I guess you are trying to do something like _.coerce[B] ?
    But again, it's discouraged to write instances like this; it's better to just derive the instances for the types you need explicitly.
    Nikita
    @joomalungma
    @carymrobbins thank you for the replay. Explicitly is ok for me.
    Jeff May
    @jeffmay
    I'm having issues with Intellij IDEA syntax highlighting the value inside of the @newtype annotated case class... is that to be expected? Does anyone have any suggestions for how folks deal with this?
    Cary Robbins
    @carymrobbins
    @jeffmay example?
    Cheng Wang
    @polarker
    Hi all, I am wondering how the overhead of asInstanceOf is eliminated since newtype is based on tagged type ?
    Is it because that type erase will convert the conversion to T.asInstanceOf[T] ?
    Cary Robbins
    @carymrobbins
    asInstanceOf won't appear in the bytecode. I did a talk a while back on how all of this works that may clear things up for you (pinned to the newtype section) - https://youtu.be/xJh3rBWcM2c?t=522
    @polarker ^^
    Vasile Gorcinschi
    @vasigorc
    Hello everyone 👋 I wanted to bring estatico/scala-newtype#69 here as well as discussions here seem to be more lively. Basically the question is what is the standard way to work around the No ClassTag available for with a @newtype ?
    gladiator99
    @gladiator99:matrix.org
    [m]
    there is no artifact for scala 3?