/* * Copyright (C) 2007-2020 Artima, Inc. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Automatically generated Scala interpreter transcript from: * * Programming in Scala (Fifth Edition) * by Martin Odersky, Lex Spoon, Bill Venners, and Frank Sommers * * http://booksites.artima.com/programming_in_scala_5ed */ scala> class Queue[T](elems: List[T]) { def head = elems.head def tail = elems.tail def enqueue(x: T) = new Queue(elems ::: List(x)) override def toString() = elems.mkString("Queue(", ", ", ")") } defined class Queue scala> def Queue[T](elems: T*) = new Queue(elems.toList) Queue: [T](elems: T*)Queue[T] scala> class Queue[T] private ( private val leading: List[T], private val trailing: List[T] ) defined class Queue scala> Queue(List(1, 2), List(3)) res0: Queue[List[Int]] = Queue(List(1, 2), List(3)) scala> def doesNotCompile(q: Queue) = {} ^ error: class Queue takes type parameters scala> def doesCompile(q: Queue[AnyRef]) = {} doesCompile: (q: Queue[AnyRef])Unit scala> val a1 = Array("abc") a1: Array[String] = Array(abc) scala> val a2: Array[Any] = a1 ^ error: type mismatch; found : Array[String] required: Array[Any] Note: String <: Any, but class Array is invariant in type T. You may wish to investigate a wildcard type such as `_ <: Any`. (SLS 3.2.10)