From 44af94a8973d3c18dcc19ff70728938a09d5e195 Mon Sep 17 00:00:00 2001 From: Nikita Pedorich Date: Mon, 4 Jul 2022 12:16:14 +0200 Subject: [PATCH 1/4] Option.when and Option.unless added to 2.11 and 2.12 --- .../collection/compat/PackageShared.scala | 9 ++++++ .../test/scala/collection/OptionTest.scala | 32 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 compat/src/test/scala/test/scala/collection/OptionTest.scala diff --git a/compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala b/compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala index 534f8948..3dfa74d7 100644 --- a/compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala +++ b/compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala @@ -282,6 +282,9 @@ private[compat] trait PackageShared { implicit def toMapViewExtensionMethods[K, V, C <: scala.collection.Map[K, V]]( self: IterableView[(K, V), C]): MapViewExtensionMethods[K, V, C] = new MapViewExtensionMethods[K, V, C](self) + + + implicit def toOptionCompanionExtension(self: Option.type): OptionCompanionExtensionMethods = new OptionCompanionExtensionMethods(self) } class ImmutableSortedMapExtensions(private val fact: i.SortedMap.type) extends AnyVal { @@ -587,3 +590,9 @@ class MutableQueueExtensionMethods[Element](private val self: m.Queue[Element]) def enqueueAll(iter: c.Iterable[Element]): Unit = self.enqueue(iter.toIndexedSeq: _*) } + +class OptionCompanionExtensionMethods(private val self: Option.type) extends AnyVal { + def when[A](cond: Boolean)(a: => A): Option[A] = if (cond) Some(a) else None + + @inline def unless[A](cond: Boolean)(a: => A): Option[A] = when(!cond)(a) +} \ No newline at end of file diff --git a/compat/src/test/scala/test/scala/collection/OptionTest.scala b/compat/src/test/scala/test/scala/collection/OptionTest.scala new file mode 100644 index 00000000..9bf9d706 --- /dev/null +++ b/compat/src/test/scala/test/scala/collection/OptionTest.scala @@ -0,0 +1,32 @@ +package test.scala.collection + +import scala.collection.compat._ +import org.junit.Test +import org.junit.Assert._ + +class OptionTest { + + @Test + def testWhenTrue: Unit = { + val option = Option.when(true)("example") + assertEquals(option, Some("example")) + } + + @Test + def testWhenFalse: Unit = { + val option = Option.when(false)("example") + assertEquals(option, None) + } + + @Test + def testUnlessTrue: Unit = { + val option = Option.unless(true)("example") + assertEquals(option, None) + } + + @Test + def testUnlessFalse: Unit = { + val option = Option.unless(false)("example") + assertEquals(option, Some("example")) + } +} From 96a4b8c34cc33d99433fbfafd3ccaa3a41fe1cad Mon Sep 17 00:00:00 2001 From: Nikita Pedorich Date: Mon, 4 Jul 2022 12:22:31 +0200 Subject: [PATCH 2/4] Copyright added; Test reworked --- .../test/scala/collection/OptionTest.scala | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/compat/src/test/scala/test/scala/collection/OptionTest.scala b/compat/src/test/scala/test/scala/collection/OptionTest.scala index 9bf9d706..3f8da23d 100644 --- a/compat/src/test/scala/test/scala/collection/OptionTest.scala +++ b/compat/src/test/scala/test/scala/collection/OptionTest.scala @@ -1,3 +1,15 @@ +/* + * Scala (https://www.scala-lang.org) + * + * Copyright EPFL and Lightbend, Inc. + * + * Licensed under Apache License 2.0 + * (http://www.apache.org/licenses/LICENSE-2.0). + * + * See the NOTICE file distributed with this work for + * additional information regarding copyright ownership. + */ + package test.scala.collection import scala.collection.compat._ @@ -6,27 +18,31 @@ import org.junit.Assert._ class OptionTest { + private val value: String = "example" + private val some: Option[String] = Some(value) + private val none: Option[String] = None + @Test def testWhenTrue: Unit = { - val option = Option.when(true)("example") - assertEquals(option, Some("example")) + val option = Option.when(true)(value) + assertEquals(option, some) } @Test def testWhenFalse: Unit = { - val option = Option.when(false)("example") - assertEquals(option, None) + val option = Option.when(false)(value) + assertEquals(option, none) } @Test def testUnlessTrue: Unit = { - val option = Option.unless(true)("example") - assertEquals(option, None) + val option = Option.unless(true)(value) + assertEquals(option, none) } @Test def testUnlessFalse: Unit = { - val option = Option.unless(false)("example") - assertEquals(option, Some("example")) + val option = Option.unless(false)(value) + assertEquals(option, some) } } From 54bce29451086d2138caf134ccbf204f2fc6708b Mon Sep 17 00:00:00 2001 From: Nikita Pedorich Date: Mon, 4 Jul 2022 12:26:56 +0200 Subject: [PATCH 3/4] formatting --- .../scala/collection/compat/PackageShared.scala | 6 +++--- .../src/test/scala/test/scala/collection/OptionTest.scala | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala b/compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala index 3dfa74d7..3ab2ad62 100644 --- a/compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala +++ b/compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala @@ -283,8 +283,8 @@ private[compat] trait PackageShared { self: IterableView[(K, V), C]): MapViewExtensionMethods[K, V, C] = new MapViewExtensionMethods[K, V, C](self) - - implicit def toOptionCompanionExtension(self: Option.type): OptionCompanionExtensionMethods = new OptionCompanionExtensionMethods(self) + implicit def toOptionCompanionExtension(self: Option.type): OptionCompanionExtensionMethods = + new OptionCompanionExtensionMethods(self) } class ImmutableSortedMapExtensions(private val fact: i.SortedMap.type) extends AnyVal { @@ -595,4 +595,4 @@ class OptionCompanionExtensionMethods(private val self: Option.type) extends Any def when[A](cond: Boolean)(a: => A): Option[A] = if (cond) Some(a) else None @inline def unless[A](cond: Boolean)(a: => A): Option[A] = when(!cond)(a) -} \ No newline at end of file +} diff --git a/compat/src/test/scala/test/scala/collection/OptionTest.scala b/compat/src/test/scala/test/scala/collection/OptionTest.scala index 3f8da23d..e81dfdda 100644 --- a/compat/src/test/scala/test/scala/collection/OptionTest.scala +++ b/compat/src/test/scala/test/scala/collection/OptionTest.scala @@ -18,7 +18,7 @@ import org.junit.Assert._ class OptionTest { - private val value: String = "example" + private val value: String = "example" private val some: Option[String] = Some(value) private val none: Option[String] = None From ed5116d5da18d1f8f9b8a1d7cbc51f350350e4db Mon Sep 17 00:00:00 2001 From: Nikita Pedorich Date: Mon, 4 Jul 2022 12:30:49 +0200 Subject: [PATCH 4/4] Rename self to fact --- .../scala/collection/compat/PackageShared.scala | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala b/compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala index 3ab2ad62..b905aa02 100644 --- a/compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala +++ b/compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala @@ -283,8 +283,8 @@ private[compat] trait PackageShared { self: IterableView[(K, V), C]): MapViewExtensionMethods[K, V, C] = new MapViewExtensionMethods[K, V, C](self) - implicit def toOptionCompanionExtension(self: Option.type): OptionCompanionExtensionMethods = - new OptionCompanionExtensionMethods(self) + implicit def toOptionCompanionExtension(fact: Option.type): OptionCompanionExtensionMethods = + new OptionCompanionExtensionMethods(fact) } class ImmutableSortedMapExtensions(private val fact: i.SortedMap.type) extends AnyVal { @@ -591,7 +591,7 @@ class MutableQueueExtensionMethods[Element](private val self: m.Queue[Element]) self.enqueue(iter.toIndexedSeq: _*) } -class OptionCompanionExtensionMethods(private val self: Option.type) extends AnyVal { +class OptionCompanionExtensionMethods(private val fact: Option.type) extends AnyVal { def when[A](cond: Boolean)(a: => A): Option[A] = if (cond) Some(a) else None @inline def unless[A](cond: Boolean)(a: => A): Option[A] = when(!cond)(a)