This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Order extends App { | |
trait Order { | |
val quantity: Int | |
val printQuantity: String = s"This order has a quantity of $quantity" | |
} | |
class Buy(q: Int) extends Order { | |
override val quantity: Int = q | |
} | |
class Sell(q: Int) extends Order { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Order.Order | |
object OrderWithWorkaround extends App { | |
class BuyWithWorkaround(q: Int) extends { | |
val quantity = q | |
} with Order | |
class SellWithWorkaround(q: Int) extends { | |
val quantity = q |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object TraitParameters extends App { | |
trait Order(quantity: Int) { | |
val printQuantity: String = s"This order has a quantity of $quantity" | |
} | |
class Buy(q: Int) extends Order(q) | |
class Sell(q: Int) extends Order(q) | |
object Trade { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.util.Random | |
object TransparentTrait { | |
val Sell = "sell" | |
val Buy = "buy" | |
transparent trait Id { | |
def id: Int | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open class Promotion { | |
def promotionDiscountFactor: Int = 2 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StudentPromotion extends Promotion { | |
override def promotionDiscountFactor: Int = 4 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object CompanySalaryApplication: | |
case class Employee(name: String, salary: Int) | |
case class CompanyBonus(amount: Int) | |
trait SalaryComputation: | |
def computeSalary(employee: Employee)(using companyBonus: CompanyBonus): Int = | |
employee.salary + companyBonus.amount |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import CompanySalaryApplication._ | |
object CompanyABonus: | |
given CompanyBonus = CompanyBonus(1000) | |
object CompanyA extends App with SalaryComputation: | |
import CompanyABonus.given | |
val jack = Employee("Jack", 8000) | |
val jill = Employee("Jill", 10000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import CompanySalaryApplication._ | |
object CompanyBBonus: | |
given companyBonus: CompanyBonus = CompanyBonus(2000) | |
object CompanyB extends App with SalaryComputation: | |
import CompanyBBonus.given | |
val julie = Employee("Julie", 8000) | |
val julia = Employee("Julia", 10000) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.template | |
trait BinarySearchTree[+T] { | |
def insert[R >: T](element: R)(implicit ord: Ordering[R]): BinarySearchTree[R] | |
def isEmpty: Boolean | |
} | |
case class Node[+T](value: T, left: BinarySearchTree[T], right: BinarySearchTree[T]) extends BinarySearchTree[T] { | |
override def isEmpty: Boolean = false | |
override def insert[R >: T](element: R)(implicit ord: Ordering[R]): BinarySearchTree[R] = { |
OlderNewer