Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lippo
committed
Dec 31, 2020
1 parent
d4de968
commit fe7e37f
Showing
2 changed files
with
32 additions
and
31 deletions.
There are no files selected for viewing
42 changes: 19 additions & 23 deletions
42
core/src/main/scala/io/github/scalaquest/core/model/Interpreter.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |
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