Skip to content

Instantly share code, notes, and snippets.

@rupeshtr78
Created November 4, 2020 17:43
Show Gist options
  • Save rupeshtr78/dd5595b864c7e3901fedf962c9d145e2 to your computer and use it in GitHub Desktop.
Save rupeshtr78/dd5595b864c7e3901fedf962c9d145e2 to your computer and use it in GitHub Desktop.
trait GeneralTrait {
def f: Int
}
class BaseImpl extends GeneralTrait {
override def f: Int = 5
}
class BaseImpl2 extends GeneralTrait {
override def f: Int = 50
}
trait StackTrait extends GeneralTrait {
abstract override def f: Int = super.f + 6
}
val s1 = (new BaseImpl).f //== 5
val s2= (new BaseImpl with StackTrait).f //== 11
val s3= (new BaseImpl2 with StackTrait).f //== 56
////////////////////////////////////////////////////////////////////////////
abstract class BaseClassImpl {
def f(x:Int): List[Int]
}
class MoreImpl extends BaseClassImpl {
def u: String = "whatever functionality else"
override def f(x:Int) = List(x)
}
trait IncrementList extends BaseClassImpl {
abstract override def f(x:Int) = {super.f(x) :+ x+1}
}
trait DoubleList {
this:BaseClassImpl =>
override def f(x:Int) = List(x) :+ x *100
}
val n2 = (new MoreImpl).u //whatever functionality else
val n21 = (new MoreImpl).f(100) // List(100)
val n3 = (new MoreImpl with IncrementList).f(100) //List(100, 101)
val n3 = (new MoreImpl with DoubleList).f(100) //List(100, 10000)
////////////////////////////////////////////////////////////////////////////////////
}
//Error: method put in class IntQueue is accessed from super.
// It may not be abstract unless it is overridden by a member declared `abstract' and `override'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment