Skip to content

Instantly share code, notes, and snippets.

@yadavan88
Last active April 4, 2022 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yadavan88/956ddc63b81f4509bc9dbc7d381b23e6 to your computer and use it in GitHub Desktop.
Save yadavan88/956ddc63b81f4509bc9dbc7d381b23e6 to your computer and use it in GitHub Desktop.
Code Samples explaining Duck typing/Structural Typing in Scala. The blog with explanation is available here: https://yadukrishnan.hashnode.dev/duck-typing-in-scala
object DuckTyping {
case class Bird(name: String, color: String) {
def fly(): Unit = println(s"A bird `$name` is flying! ")
}
case class Aeroplane(make: String, airline: String) {
def fly(): Unit = println(s"$airline is flying $make aeroplane")
}
case class Starship(name: String, captain: String, magicWord: String) {
def fly(): Unit = println(
s"The starship `$name` goes into warp when Captain $captain says `$magicWord`"
)
}
def letsFly[T <: { def fly(): Unit }](obj: T): Unit = obj.fly()
def run() = {
val bird = Bird("Eagle", "Grey")
letsFly(bird)
val aeroplane = Aeroplane("Airbus", "Lufthansa")
letsFly(aeroplane)
val starship = Starship("Enterprise", "Picard", "Engage")
letsFly(starship)
}
}
object DucktypingDemo extends App {
DuckTyping.run()
}
object Inheritence {
trait Flyable {
def fly(): Unit
}
case class Bird(name: String, color: String) extends Flyable {
override def fly(): Unit = println(s"[OOP] A bird `$name` is flying! ")
}
case class Aeroplane(make: String, airline: String) extends Flyable {
override def fly(): Unit = println(
s"[OOP] $airline is flying $make aeroplane"
)
}
case class Starship(name: String, captain: String, magicWord: String)
extends Flyable {
override def fly(): Unit = println(
s"[OOP] The starship `$name` goes into warp when Captain $captain says `$magicWord`"
)
}
def letsFly(f: Flyable) = f.fly()
def run(): Unit = {
val bird = Bird("Eagle", "Grey")
letsFly(bird)
val aeroplane = Aeroplane("Airbus", "Lufthansa")
letsFly(aeroplane)
val starship = Starship("Enterprise", "Picard", "Engage")
letsFly(starship)
}
}
object InheritenceDemo extends App {
Inheritence.run()
}
import java.time.LocalDate
object SimpleStructure extends App {
case class Country(name: String, code: String)
case class Person(name: String, dob: LocalDate)
def printName[A <: { val name: String }](obj: A) = println(
"The name from the object is : " + obj.name
)
val india = Country("India", "IN")
val sachin = Person("Sachin", LocalDate.parse("1973-04-24"))
printName(india)
printName(sachin)
type NameLike = {
val name: String
}
def printName2(obj: NameLike) = {
println("Name is : " + obj)
}
printName2(india)
printName2(sachin)
}
object TypeClasses {
case class Bird(name: String, color: String)
case class Aeroplane(make: String, airline: String)
case class Starship(name: String, captain: String, magicWord: String)
trait CanFly[A] {
def fly(obj: A): Unit
}
implicit val birdFly: CanFly[Bird] = new CanFly[Bird] {
def fly(bird: Bird): Unit = println(s"[TC] A bird `${bird.name}` is flying")
}
implicit val aeroFly: CanFly[Aeroplane] = new CanFly[Aeroplane] {
def fly(a: Aeroplane): Unit = println(
s"[TC] ${a.airline} is flying ${a.make} aeroplane"
)
}
implicit val starshipFly: CanFly[Starship] = new CanFly[Starship] {
def fly(s: Starship): Unit = println(
s"[TC] The starship `${s.name}` goes into warp when Captain ${s.captain} says `${s.magicWord}`"
)
}
def letsFly[A](obj: A)(implicit c: CanFly[A]): Unit = c.fly(obj)
def run(): Unit = {
val bird = Bird("Eagle", "Grey")
letsFly(bird)
val aeroplane = Aeroplane("Airbus", "Lufthansa")
letsFly(aeroplane)
val starship = Starship("Enterprise", "Picard", "Engage")
letsFly(starship)
}
}
object TypeclassDemo extends App {
TypeClasses.run()
}
@yadavan88
Copy link
Author

yadavan88 commented Apr 4, 2022

This code sample is used in the blog link:
https://yadukrishnan.hashnode.dev/duck-typing-in-scala

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment