Skip to content

Commit

Permalink
add ScalaAdvance/ContextBound1/, update implicit demo, readme
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Oct 30, 2021
1 parent 5ba3e4f commit d4347ac
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@
- [Implicit_Demo1](./src/main/scala/ScalaBasic/Implicit_Demo1.scala)
- [Implicit_Demo2](./src/main/scala/ScalaBasic/Implicit_Demo2.scala)
- [Implicit_Demo3](./src/main/scala/ScalaBasic/Implicit_Demo3.scala)
- [Implicit_Demo4](./src/main/scala/ScalaBasic/Implicit_Demo4.scala)
- [Implicit_Demo4](./src/main/scala/ScalaBasic/Implicit_Demo4.scala) : implicit parameter
- [Implicit_Demo5](./src/main/scala/ScalaBasic/Implicit_Demo5.scala)
- [Implicit_Demo6](./src/main/scala/ScalaBasic/Implicit_Demo6.scala)
- [Implicit_Demo7](./src/main/scala/ScalaBasic/Implicit_Demo7.scala)
Expand Down
67 changes: 67 additions & 0 deletions src/main/scala/ScalaAdvance/ContextBound1/demo1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package ScalaAdvance.ContextBound1

// https://www.bilibili.com/video/BV12N411R726?p=280

/**
* Context bounds demo 1 :
*
* 1) similar as view bounds, context bounds is also a `language sugar` of implicit parameters
*
*
* Note : Difference between `Ordered` and `Ordering`:
*
* -> Ordered : inherits from java's Comparable interface,
* Comparable is an "internal" comparator, override CompareTo method
*
* -> Ordering : inherits from java's Comparator interface,
* Comparator is an "external" comparator, (need to implement a class for it)
*
*/

object demo1 {

// here we define a implicit value
// ref : https://github.com/yennanliu/utility_Scala/blob/master/src/main/scala/ScalaBasic/Implicit_Demo4.scala
implicit val personComparator = new Ordering[Person]{
override def compare(p1: Person, p2: Person): Int = {
p1.age - p2.age
}
}

// entry point
def main(args: Array[String]): Unit = {

// -------------------
// demo 1
// -------------------
val p1 = new Person("JAY",19)
val p2 = new Person("lynn",20)
val compareComm4 = new CompareComm4(p1, p2)
println(compareComm4.getter)

}
}

// a simple Person class
class Person(val name:String, val age:Int) {
// method
override def toString: String = {
this.name + "\t" + this.age
}
}

/**
* Approach 1)
*
* 1) [T:Ordering] : generic type
* 2) accept T type object
* 3) implicit comparator:Ordering[T] : implicit parameter
* -> if there is any obj with type Ordering[T] in code, then such obj will be sent to this class automatically
*/
class CompareComm4[T:Ordering](obj1:T, obj2:T)(implicit comparator:Ordering[T]){
def getter = {
if (comparator.compare(obj1, obj2) > 0 ){
obj1
}else obj2
}
}
8 changes: 7 additions & 1 deletion src/main/scala/ScalaBasic/Implicit_4.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ package ScalaBasic

// https://www.youtube.com/watch?v=smtulYDn7OU&list=PLmOn9nNkQxJEqCNXBu5ozT_26xwvUbHyE&index=172

/**
* Implicit Demo 4 :
*
* 1) implicit parameter
*/


object Implicit_4 extends App {
// run
val db1 = new DB1(new MySql1)
Expand All @@ -14,7 +21,6 @@ object Implicit_4 extends App {
*
* 1) will make a .class implicitClass$DB1$2 under the hood
* 2) and also make a method : DB1$1 // via implicitClass$DB1$2 constructor, return implicit class instance
*
*/
// implicit class
implicit class DB1(val m: MySql1) {
Expand Down

0 comments on commit d4347ac

Please sign in to comment.