Skip to content

Commit

Permalink
Support Yield.From in functions that return SeqView for Scala 2.11 an…
Browse files Browse the repository at this point in the history
…d 2.13
  • Loading branch information
Atry committed Jan 29, 2020
1 parent f5406c6 commit acd1a4b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
Expand Up @@ -3,6 +3,7 @@ package com.thoughtworks.dsl.keywords
import com.thoughtworks.dsl.Dsl
import com.thoughtworks.dsl.Dsl.Keyword
import com.thoughtworks.dsl.keywords.Yield.From
import com.thoughtworks.enableIf
import com.thoughtworks.enableMembersIf

import scala.collection._
Expand Down Expand Up @@ -68,6 +69,17 @@ private[keywords] object YieldScalaVersions {

trait LowPriorityYield1 extends LowPriorityYield3 {

@enableIf(scala.util.Properties.versionNumberString.matches("""^2\.11\..*$"""))
implicit def seqViewYieldFromDsl[A, FromCollection <: Traversable[A], Coll1, Coll2](
implicit canBuildFrom: CanBuildFrom[SeqView[A, Coll1], A, SeqView[A, Coll2]])
: Dsl[From[FromCollection], SeqView[A, Coll1], Unit] =
new Dsl[From[FromCollection], SeqView[A, Coll1], Unit] {
def cpsApply(keyword: From[FromCollection], generateTail: Unit => SeqView[A, Coll1]): SeqView[A, Coll1] = {
(keyword.elements.toSeq.view ++: generateTail(()))(canBuildFrom).asInstanceOf[SeqView[A, Coll1]]
}
}

@enableIf(scala.util.Properties.versionNumberString.matches("""^2\.12\..*$"""))
implicit def seqViewYieldFromDsl[A, FromCollection <: Traversable[A], Coll1, Coll2](
implicit canBuildFrom: CanBuildFrom[SeqView[A, Coll1], A, SeqView[A, Coll2]])
: Dsl[From[FromCollection], SeqView[A, Coll1], Unit] =
Expand Down Expand Up @@ -122,6 +134,20 @@ private[keywords] object YieldScalaVersions {
}
}

implicit def indexedSeqViewYieldFromDsl[A, FromCollection <: View.SomeIterableOps[A]]: Dsl[From[FromCollection], IndexedSeqView[A], Unit] =
new Dsl[From[FromCollection], IndexedSeqView[A], Unit] {
def cpsApply(keyword: From[FromCollection], generateTail: Unit => IndexedSeqView[A]): IndexedSeqView[A] = {
new IndexedSeqView.Concat(keyword.elements.toIndexedSeq, generateTail(()))
}
}

implicit def seqViewYieldFromDsl[A, FromCollection <: View.SomeIterableOps[A]]: Dsl[From[FromCollection], SeqView[A], Unit] =
new Dsl[From[FromCollection], SeqView[A], Unit] {
def cpsApply(keyword: From[FromCollection], generateTail: Unit => SeqView[A]): SeqView[A] = {
new SeqView.Concat(keyword.elements.toSeq, generateTail(()))
}
}

implicit def seqViewYieldDsl[A, B >: A]: Dsl[Yield[A], SeqView[B], Unit] =
new Dsl[Yield[A], SeqView[B], Unit] {
def cpsApply(keyword: Yield[A], generateTail: Unit => SeqView[B]): SeqView[B] = {
Expand Down
Expand Up @@ -7,12 +7,13 @@ import scala.annotation.tailrec
import scala.collection.{LinearSeq, SeqView}
import scala.runtime.NonLocalReturnControl
import org.scalatest.freespec.AnyFreeSpec
import org.scalatest.Assertions
import org.scalatest.matchers.should.Matchers

/**
* @author 杨博 (Yang Bo)
*/
class YieldSpec extends AnyFreeSpec with Matchers {
class YieldSpec extends AnyFreeSpec with Matchers with Assertions {

{
!Yield(1)
Expand Down Expand Up @@ -303,9 +304,28 @@ class YieldSpec extends AnyFreeSpec with Matchers {

"view" - {

def shouldCompile = {
!Yield("naked")
Vector.empty[String].view
"indexed seq view" in {
def generator = {
!Yield("naked")
Vector.empty[String].view
}
assert(generator.toList == List("naked"))
}

"yield from indexed seq view" in {
def generator = {
!Yield(100, 101)
Vector.empty[Int].view
}
assert(generator.toList == List(100, 101))
}

"yield from seq view" in {
def generator = {
!Yield(100, 101)
Seq.empty[Int].view
}
assert(generator.toList == List(100, 101))
}

"local method" in {
Expand Down

0 comments on commit acd1a4b

Please sign in to comment.