Skip to content

Create scala.yml #3

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
22 changes: 22 additions & 0 deletions .github/workflows/scala.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Scala CI

on:
pull_request:
branches: [ master ]

jobs:
build:

runs-on: self-hosted

steps:
- uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
java-version: '11'
distribution: 'adopt'
- name: Run tests
run: sbt test
- name: Run tests
run: sbt dist
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Scala code examples


This repository has my learning scala examples

Expand Down
92 changes: 0 additions & 92 deletions src/main/scala/StructuralTypeApp.scala
Original file line number Diff line number Diff line change
@@ -1,93 +1 @@
object StructuralTypeApp extends App {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please tell me why you have removed this?

//structural types
type JavaCloseable = java.io.Closeable
class HipsterCloseable {
def close(): Unit = println("hipster closeable")
def closeSilently(): Unit = println("hipster silently closeable")
}

//def closeQuietly(closeable: HipsterCloseable OR JavaCloseable): Unit
type UnifiedCloseable = {
def close(): Unit
} // STRUCTURAL TYPE - Allow working with unrelated type that has common structure

def closeQuietly(closeable: UnifiedCloseable): Unit = closeable.close()
closeQuietly(new JavaCloseable {
override def close(): Unit = println("JavaCloseable...")
})
closeQuietly(new HipsterCloseable)

//TYPE REFINEMENT- Refine existing type(e.g JavaCloseable) with structural type
type AdvanceCloseable = JavaCloseable {
def closeSilently(): Unit
} //Enrich type from JavaCloseable

class AdvanceJavaCloseable extends JavaCloseable {
override def close(): Unit = println("java's close")
def closeSilently(): Unit = {
close()
println("java closes silently")
}
}
def closeShh(advanceJavaCloseable: AdvanceJavaCloseable) =
advanceJavaCloseable.closeSilently()
closeShh(new AdvanceJavaCloseable)
//closeShh(new HipsterCloseable) // will not compile because HipsterCloseable doesn't originated from JavaCloseable even though it has all the methods
//Using structural types as standalone type
def altClose(closeable: { def close(): Unit }) = closeable.close()

//type-checking => Duck typing
type SoundMaker = {
def makeSound(): Unit
}
class Dog {
def makeSound() = println("bark!")
}
class Cat {
def makeSound() = println("vroom!")
}
val dog: SoundMaker = new Dog
val cat: SoundMaker = new Cat

//CAVEAT-structural and refinement type are based on reflection

trait `CBL(Cons based list)`[+T] {
def head: T
def tail: `CBL(Cons based list)`[T]
}
class Human {
def head: Brain = new Brain
}
class Brain {
override def toString: String = "BRAINZ!"
}

def f[T](somethingWithAHead: { def head: T }) =
println(somethingWithAHead.head)
/*
f is compatible with a CBL and Human? YES.
*/
case object CBLNIl extends `CBL(Cons based list)`[Nothing] {
override def head: Nothing = ???
override def tail: `CBL(Cons based list)`[Nothing] = ???
}

case class CBCOns[T](override val head: T,
override val tail: `CBL(Cons based list)`[T])
extends `CBL(Cons based list)`[T]
f(CBCOns(12, CBLNIl))
f(new Human)

object HeadEqualizer {
type Headable[T] = { def head: T }
def ===[T](a: Headable[T], b: Headable[T]): Boolean = a.head == b.head
/*
is compatible with a CBL and Human? Yes
*/
val brainzList = CBCOns(new Brain, CBLNIl)
val stringsList = CBCOns("Brainz", CBLNIl)
HeadEqualizer.===(brainzList, new Human) //Problem
HeadEqualizer.===(new Human, stringsList) // It is not right and not type safe
}

}