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
trait DatabaseOperations: | |
def createDatabase(): Unit | |
def manageDatabase(): Unit | |
def maintainDatabase(): Unit | |
class CloudProvider extends DatabaseOperations: | |
def createDatabase(): Unit = | |
println("Cloud Provider creates the database.") | |
def manageDatabase(): Unit = | |
println("Cloud Provider manages the database.") |
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.quoted.{quotes, Expr, Quotes, Type} | |
inline def compareEntity[T](a: T, b: T): Int = | |
${compareEntitiesImpl('a, 'b)} | |
def compareEntitiesImpl[T: Type](a: Expr[T], b: Expr[T])(using Quotes): Expr[Int] = { | |
import quotes.reflect.* | |
val tpe = TypeRepr.of[T] | |
val fields = tpe.typeSymbol.caseFields |
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.quoted.* | |
inline def compareEntity[T](a: T, b: T): Boolean = | |
${compareEntitiesImpl('a, 'b)} | |
def compareEntitiesImpl[T: Type](a: Expr[T], b: Expr[T])(using Quotes): Expr[Boolean] = { | |
import quotes.reflect.* | |
val tpe = TypeRepr.of[T] | |
val fields = tpe.typeSymbol.caseFields |
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
@main def main(): Unit = | |
println(isUgly(14)) | |
private val primes = List(2, 3, 5) | |
def isUgly(n: Int): Boolean = | |
var result = n | |
for (prime <- primes) | |
while (result > 1 && result % prime == 0) | |
result /= prime |
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
// Define a trait for a generic Database Access Object | |
trait UserDao { | |
def getUser(id: Int): String | |
} | |
// Concrete implementation of UserDao | |
class UserDaoImpl extends UserDao { | |
def getUser(id: Int): String = s"User with ID $id" | |
} |
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
@main def main(): Unit = | |
// Implicit parameter declaration | |
implicit val defaultMultiplier: Int = 2 | |
// Function taking implicit parameter | |
def multiplyBy(x: Int)(implicit multiplier: Int): Int = x * multiplier | |
// Function call without explicitly passed parameter | |
val result = multiplyBy(10) // compiler injects defaultMultiplier automatically |
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
@main def main(): Unit = | |
val value1 = true | |
val value2 = 1234L | |
val value3 = "Hello Scala 3!" | |
println(process(value1)) // "Received a Boolean: true" | |
println(process(value2)) // "Received a Long: 123" | |
println(process(value3)) // "Hello Scala 3!" | |
type ExternalType1 = java.lang.Boolean | |
type ExternalType2 = java.lang.Long |
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 java.util.Objects; | |
private record AddressIdentity( | |
String city, | |
String streetName, | |
String streetNumber | |
) {} | |
void main() { | |
var first = new AddressIdentity("Kyiv", "Babusia", "12345"); |
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 java.util.ArrayList; | |
import java.util.List; | |
void main() { | |
System.out.println("Hello, Java 22!"); | |
QueueOperations queue = createQueue(createStack(), createStack()); | |
queue.push(1); | |
queue.push(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
package org.example | |
fun main() { | |
createQueue(createStack(), createStack()).apply { | |
push(1) | |
push(2) | |
push(3) | |
pop() | |
peek() | |
pop() |
NewerOlder