Last active
December 11, 2015 04:18
-
-
Save andypetrella/4543778 to your computer and use it in GitHub Desktop.
Un exemple de DSL très simple avec Scala
This file contains hidden or 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
case class Banque(nom:String) {banque => | |
def accepte(m:Montant) = | |
CompteBuilder(banque, m) | |
} | |
implicit def stringToBanque(s:String) = Banque(s) | |
implicit def intWithDevise(i:Int) = new { | |
def ~(d:Devise) = Montant(i, d) | |
} | |
case class CompteBuilder(b:Banque, m:Montant) { | |
def sur(compte:String) = Compte(b, compte).plus(m) | |
} | |
trait Devise | |
case object Euro extends Devise | |
case object Pound extends Devise | |
case class Compte(banque:Banque, numero:String, situation:Situation=ZERO) { | |
def plus(montant:Montant) = this.copy(situation = situation + montant) | |
def moins(montant:Montant) = plus(-montant) | |
} | |
case class Montant(somme:Int, d:Devise) { | |
def +(other:Montant) = this.copy(somme=other.somme+somme) //ignoring the devise in this example | |
def unary_- = this.copy(somme = -somme) | |
} | |
case class Situation(montant:Montant) { | |
def +(m:Montant) = this.copy(montant=montant + m) | |
} | |
case object ZERO extends Situation(Montant(0, Euro)) |
This file contains hidden or 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
"XXX" accepte 1000~Euro sur "001-345657678-23" plus 200~Euro moins 50~Euro |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Plusieurs erreurs sont levées lors de la compilation au niveau des "implicit def ..." (lignes 8, 10).
Comment peut-on y remédier ?