Skip to content

Commit

Permalink
Fix #1091 (@mschuwalow idea) (#1094)
Browse files Browse the repository at this point in the history
* Fix #1091

Fix #1091

* remove classtag

* Clean

* doc

* scalafmt

* clean

* Improve test

* missing final

* Fix compilation

* Fix tests compilation

* fix tests

* Fix comment

* Fix compilation

* Test @mschuwalow idea

* fix test

* Test #1094 (comment)

* Improve tests

* scalafmt

* Fix tests

* Fix compilation

* Fix compilation

* Fix test && scalafmt

* scalafix

* trigger CI

* Try fix Scala3 tests

* Prefer 60
  • Loading branch information
guizmaii committed Mar 7, 2023
1 parent 2ca83df commit 519b1b8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
4 changes: 2 additions & 2 deletions benchmarks/src/main/scala/zio/prelude/ForEachBenchmark.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ class ForEachBenchmarks {

@Benchmark
def zioForEachZIO(bh: Blackhole): Unit =
bh.consume(unsafeRun(list.forEach(ZIO.succeedNow(_))))
bh.consume(unsafeRun(list.forEach(ZIO.succeed(_))))

@Benchmark
def zioForEach_ZIO(bh: Blackhole): Unit =
bh.consume(unsafeRun(list.forEach_(ZIO.succeedNow(_))))
bh.consume(unsafeRun(list.forEach_(ZIO.succeed(_))))

@Benchmark
def zioForEachZPure(bh: Blackhole): Unit =
Expand Down
46 changes: 37 additions & 9 deletions core-tests/shared/src/test/scala/zio/prelude/NewtypeSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package zio.prelude

import zio.NonEmptyChunk
import zio.prelude.NewtypeSpecTypes._
import zio.prelude.laws._
import zio.prelude.laws.{isFailureV, isSuccessV}
import zio.prelude.newtypes.{And, Or, Sum}
import zio.test.Assertion.{equalTo => _, _}
import zio.test.Assertion._
import zio.test.TestAspect._
import zio.test.{Assertion => TestAssertion, _}

import scala.reflect.ClassTag
Expand Down Expand Up @@ -62,9 +63,12 @@ object NewtypeSpec extends ZIOBaseSpec {
isFailureV(equalTo(NonEmptyChunk("-1 did not satisfy greaterThanOrEqualTo(0)")))
)
},
test("can summon classtag for newtype") {
test("implicitly classtag summoning for newtype")(
assertZIO(typeCheck("implicitly[ClassTag[LuckyNumber]]"))(isRight)
},
),
test("classtag reports same runtimeclass as underlying primitive") {
assert(implicitly[ClassTag[LuckyNumber]].runtimeClass eq implicitly[ClassTag[Double]].runtimeClass)(isTrue)
} @@ exceptScala211,
test("allows creating subtypes of newtypes") {
val compile = typeCheck {
"""import java.util.UUID
Expand All @@ -82,17 +86,41 @@ object NewtypeSpec extends ZIOBaseSpec {
data(1) = Natural(1)
assertTrue(data.toList === List(Natural(0): Natural, Natural(1): Natural))
},
test("classtag reports same runtimeclass as underlying primitive") {
assertTrue(implicitly[ClassTag[Natural]].runtimeClass === implicitly[ClassTag[Int]].runtimeClass)
test("pattern matching") {
val number = LuckyNumber(10.0)
assertTrue(
(number match {
case LuckyNumber(10.0) => true
case _ => false
}) && (number match {
case LuckyNumber(20.0) => false
case _ => true
})
)
}
),
suite("Subtype")(
test("subtypes values") {
val two = 2
assertTrue(two + Natural.two == 2 + 2)
},
test("can summon classtag for subtype") {
test("implicitly classtag summoning for subtype")(
assertZIO(typeCheck("implicitly[ClassTag[Natural]]"))(isRight)
),
test("classtag reports same runtimeclass as underlying primitive") {
assert(implicitly[ClassTag[Natural]].runtimeClass eq implicitly[ClassTag[Int]].runtimeClass)(isTrue)
} @@ exceptScala211,
test("pattern matching") {
val number = Natural(2)
assertTrue(
(number match {
case Natural(2) => true
case _ => false
}) && (number match {
case Natural(3) => false
case _ => true
})
)
}
),
suite("examples from documentation")(
Expand All @@ -103,10 +131,10 @@ object NewtypeSpec extends ZIOBaseSpec {
assert(Meter.unwrap(z))(equalTo(3.4 + 4.3))
},
test("exists") {
assert(exists(List(true, false))(identity))(isTrue)
assert(exists(List(true, false))(scala.Predef.identity))(isTrue)
},
test("forall") {
assert(forall(List(true, false))(identity))(isFalse)
assert(forall(List(true, false))(scala.Predef.identity))(isFalse)
},
test("sumInt") {
val actual = sum(List(1, 2, 3))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ trait InvariantVersionSpecific {
*/
implicit def IterableForEach[F[+a] <: Iterable[a]](implicit derive: DeriveCanBuildFrom[F]): ForEach[F] =
new ForEach[F] {
def forEach[G[+_]: IdentityBoth: Covariant, A, B](fa: F[A])(f: A => G[B]): G[F[B]] =
def forEach[G[+_]: IdentityBoth: Covariant, A, B](fa: F[A])(f: A => G[B]): G[F[B]] =
CovariantIdentityBoth[G].forEach(fa)(f)(derive.derive)
override def forEach_[G[+_]: IdentityBoth: Covariant, A](fa: F[A])(f: A => G[Any]): G[Unit] =
CovariantIdentityBoth[G].forEach_(fa)(f)
Expand Down
6 changes: 2 additions & 4 deletions core/shared/src/main/scala-2/zio/prelude/Newtype.scala
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ abstract class Newtype[A] extends NewtypeVersionSpecific {
*/
def wrapAll[F[_]](value: F[A]): F[Type] = macro zio.prelude.Macros.wrapAll_impl[F, A, Type]

implicit def classTag(implicit underlying: ClassTag[A]): ClassTag[Type] = Newtype.classTag
implicit def classTag(implicit underlying: ClassTag[A]): ClassTag[Type] = underlying.asInstanceOf[ClassTag[Type]]
}

object Newtype {
Expand Down Expand Up @@ -324,7 +324,5 @@ object Newtype {
}

implicit def classTag[N <: Newtype[_]: ClassTagWrapper]: ClassTag[N#Type] =
new ClassTag[N#Type] {
def runtimeClass: Class[_] = ClassTagWrapper[N].classTag.runtimeClass
}
ClassTagWrapper[N].classTag.asInstanceOf[ClassTag[N#Type]]
}
5 changes: 1 addition & 4 deletions core/shared/src/main/scala-3/zio/prelude/Newtype.scala
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,7 @@ abstract class NewtypeCustom[A] {
)
}.as(value.asInstanceOf[F[Type]])

implicit def classTag(implicit underlying: ClassTag[A]): ClassTag[Type] =
new ClassTag[Type] {
val runtimeClass = underlying.runtimeClass
}
implicit def classTag(implicit underlying: ClassTag[A]): ClassTag[Type] = underlying.asInstanceOf[ClassTag[Type]]
}

abstract class SubtypeCustom[A] extends NewtypeCustom[A] {
Expand Down

0 comments on commit 519b1b8

Please sign in to comment.