Skip to content

Commit

Permalink
Funzionante!
Browse files Browse the repository at this point in the history
  • Loading branch information
Lippo committed Dec 31, 2020
1 parent d4de968 commit fe7e37f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
@@ -1,34 +1,30 @@
package io.github.scalaquest.core.model

import io.github.scalaquest.core.model.Model.ModelTypes

abstract class Interpreter[M <: Model](implicit modelTypes: ModelTypes[M]) {
// protected val modelTypes: ModelTypes[M] = implicitly[ModelTypes[M]]

def interpret(item: modelTypes.Item): Either[String, Int]
}

class SimpleInterpreter[M <: Model](implicit modelTypes: ModelTypes[M]) extends Interpreter[M] {

override def interpret(item: modelTypes.Item): Either[String, Int] = {
Right(3)
}
abstract class Interpreter[M <: Model] {
def interpret(item: M#I): Either[String, Int]
}

object Main extends App {
val myModel = new ConcreteModel
implicit val concreteModelTypes = new ModelTypes[ConcreteModel](myModel)
val interpreter: Interpreter[ConcreteModel] = new SimpleInterpreter[ConcreteModel]

import myModel.{ConcreteItem, ConcreteState}
// La tecnica
// Estendere model con un oggetto concreto
// Importare dall'oggetto i tipi concreti
// Creare un interprete parametrizzato con $oggetto.type
import ConcreteModel.{ConcreteItem, ConcreteState}

val interpreter: ConcreteState => Interpreter[ConcreteModel.type] = state =>
item => {
println(item.color)
println(state.time)
Right(item.use(state)).map(_ => 1)
}

val items: Set[ConcreteItem] = Set(
ConcreteItem("apple"),
ConcreteItem("banana"),
ConcreteItem("cheddar")
ConcreteItem("apple", "red"),
ConcreteItem("banana", "yellow"),
ConcreteItem("cheddar", "orange")
)
val currentState = ConcreteState(items)

interpreter.interpret(items.head)
val currentState = ConcreteState(0, items)

println(interpreter(currentState).interpret(items.head))
}
21 changes: 13 additions & 8 deletions core/src/main/scala/io/github/scalaquest/core/model/Model.scala
Expand Up @@ -6,6 +6,7 @@ package io.github.scalaquest.core.model
// Compilare
// Scrivere algoritmi che usano le implementazioni
// Compilare

abstract class Model {
type S <: State

Expand All @@ -24,28 +25,32 @@ abstract class Model {

object Model {

class ModelTypes[M <: Model](val model: Model) {
type State = model.S
type Item = model.I
}
// class ModelTypes[M <: Model](val model: Model) {
// type State = model.S
// type Item = model.I
// }

// class ItemType[M <: Model](val model: Model) {
// type Item = model.I
// }
}

class ConcreteModel extends Model {
object ConcreteModel extends Model {

override type I = ConcreteItem
override type S = ConcreteState

case class ConcreteState(items: Set[ConcreteItem]) extends State {
case class ConcreteState(
time: Int,
items: Set[ConcreteItem],
hasDefeatedDragon: Boolean = false
) extends State {

override def hasItem(item: I): Boolean = true
}

case class ConcreteItem(name: String) extends Item {
override def use(state: S): ConcreteState = state
case class ConcreteItem(name: String, color: String) extends Item {
override def use(state: S): ConcreteState = state.copy(time = state.time + 1)
}

}

0 comments on commit fe7e37f

Please sign in to comment.