Skip to content

Instantly share code, notes, and snippets.

@andypetrella
Last active December 11, 2015 04:18
Show Gist options
  • Save andypetrella/4543778 to your computer and use it in GitHub Desktop.
Save andypetrella/4543778 to your computer and use it in GitHub Desktop.
Un exemple de DSL très simple avec Scala
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))
"XXX" accepte 1000~Euro sur "001-345657678-23" plus 200~Euro moins 50~Euro
Copy link

ghost commented Nov 12, 2015

Plusieurs erreurs sont levées lors de la compilation au niveau des "implicit def ..." (lignes 8, 10).
Comment peut-on y remédier ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment