Skip to content

Commit

Permalink
Make Stream covariant to match regular collections (#126)
Browse files Browse the repository at this point in the history
* Make Stream covariant to match regular collections

This should fix the weird typing issues we encountered earlier, such as not being able to view a stream of `None`s as a stream of `Option`s.

* Remove unused `Value` type member

* Add unit test for covariant behavior
  • Loading branch information
shadaj authored and PhilipAxelrod committed Nov 11, 2017
1 parent 8ad40db commit 25afed8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class PurePursuitControllerTests extends FunSuite {

val target = Point(Feet(1), Feet(1))
val position = hardware.turnPosition.mapToConstant(Point(Feet(2), Feet(2)))
val path: Stream[(Segment, Option[Segment])] = hardware.turnPosition.mapToConstant((Segment(origin, target), None))
val path = hardware.turnPosition.mapToConstant((Segment(origin, target), None))

val output = controllers.purePursuitControllerTurn(
hardware.turnPosition.mapToConstant(Degrees(45)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@ import squants.time.{Time, TimeDerivative, TimeIntegral}
import scala.collection.immutable.Queue
import scala.ref.WeakReference
import com.lynbrookrobotics.potassium.Platform
import com.lynbrookrobotics.potassium.events.{ContinuousEvent, ImpulseEvent}

abstract class Stream[T] { self =>
final type Value = T
import scala.annotation.unchecked.uncheckedVariance

abstract class Stream[+T] { self =>
val expectedPeriodicity: ExpectedPeriodicity

val originTimeStream: Option[Stream[Time]]

private[this] var listeners = Vector.empty[T => Unit]

protected def publishValue(value: T): Unit = {
protected def publishValue(value: T @uncheckedVariance): Unit = {
listeners.foreach(_.apply(value))
// TODO: more stuff maybe
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import scala.concurrent.duration.Duration
import scala.concurrent.{Await, Promise}

class StreamTest extends FunSuite {
test("Streams can be used as a stream of a parent type of the original element type (covariance)") {
val origStream: Stream[None.type] = Stream.manual[None.type]._1

assertCompiles("origStream: Stream[Option[Int]]")
}

test("Manually created stream runs callbacks appropriately") {
val (str, pub) = Stream.manual[Int]

Expand Down

0 comments on commit 25afed8

Please sign in to comment.