0
def foo[T <% Ordered[T]](array: Array[T], x: T) = {
      ........
}

So with foo I would like it to take in both Array and ArrayBuffer. However when I try and pass an ArrayBuffer, I get a type mistmatch

main.scala:67: error: type mismatch;
 found   : scala.collection.mutable.ArrayBuffer[Int]

I could simply solve this by making the array parameter an ArrayBuffer instead of an Array but that makes my method less flexible.

2 Answers 2

2

scala.collection.mutable.Seq will allow you to access and modify both Array and ArrayBuffer by index.

Note: view bound syntax is now deprecated, you can achieve the same thing with an implicit parameter:

def foo[T](array: scala.collection.mutable.Seq[T], x: T)(implicit ordering: Ordering[T]) = {
if (ordering.gt(array(0), x)) {
  array.update(0, x)
}

}

You can find more info about that in SI-7629 (https://issues.scala-lang.org/browse/SI-7629)

Hope that helps,

Thanks

Sign up to request clarification or add additional context in comments.

1 Comment

The implicit parameter you used is not equivalent to the view bound.
1

You can write

def foo[T <% Ordered[T]](seq: Seq[T], x: T) = ???

ArrayBuffer is indeed an implementation of Seq, and Array has an implicit conversion to it, so it will work just fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.