Skip to content

SCALA-5: scala generics sample code #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 31, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.baeldung.scala.generics

object GenericsIntro {

case class Apple(name: String)

def main(args: Array[String]): Unit = {
NonGenericWay.run()
}

object LongHandWay {

case class AppleMagicHat(magic: Apple)

case class RabbitMagicHat(magic: Rabbit)

def run(): Unit = {
val someHat = AppleMagicHat(Apple("gala"))
val apple: Apple = someHat.magic
println(apple.name)
}
}

object NonGenericWay {

case class MagicHat(magic: AnyRef)

def run(): Unit = {
val someHat = MagicHat(Rabbit(2))
val apple: Apple = someHat.magic.asInstanceOf[Apple]
println(apple.name)
}
}

object GenericWay {

case class MagicHat[A](magic: A)

def run(): Unit = {
val rabbitHat = MagicHat[Rabbit](Rabbit(2))
val rabbit: Rabbit = rabbitHat.magic
println(rabbit.cuteness)
}
}

case class Rabbit(cuteness: Int)

object GenericMethods {
def middle[A](input: Seq[A]): A = input(input.size / 2)

def itemsAt[A, B](index: Int, seq1: Seq[A], seq2: Seq[B]): (A, B) = (seq1(index), seq2(index))

def run() = {

val rabbits = List[Rabbit](Rabbit(2), Rabbit(3), Rabbit(7))
val middleRabbit: Rabbit = middle[Rabbit](rabbits)

val apples = List[Apple](Apple("gala"), Apple("pink lady"))
val items: (Rabbit, Apple) = itemsAt[Rabbit, Apple](1, rabbits, apples)
}

}

object NonGenericMethods {
def totalSize(list1: List[_], list2: List[_]): Int = list1.length + list2.length
def run() = {
val rabbits = List[Rabbit](Rabbit(2), Rabbit(3), Rabbit(7))
val strings = List("a", "b")
val size: Int = totalSize(rabbits, strings)
}

}

}