Skip to content

Commit

Permalink
test for @ syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
DmytroMitin committed Jul 21, 2019
1 parent 3a626db commit 7b9c604
Showing 1 changed file with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.github.dmytromitin

import org.scalatest._

class SyntaxMonoidSemigroupTest extends FlatSpec with Matchers {
@syntax
trait Semigroup[A] {
def combine(a: A, a1: A): A
}

object Semigroup {
// object syntax {
// implicit class Ops[A](a: A) {
// def combine(a1: A)(implicit inst: Semigroup[A]): A = inst.combine(a, a1)
// }
// }
}

@syntax
trait Monoid[A] extends Semigroup[A] {
def empty: A
def combine(a: A, a1: A): A
}

object Monoid {
def instance[A](f: => A, f1: (A, A) => A): Monoid[A] = new Monoid[A] {
override def empty: A = f
override def combine(a: A, a1: A): A = f1(a, a1)
}

// object syntax {
// implicit class Ops[A](a: A) {
// def combine(a1: A)(implicit inst: Monoid[A]): A = inst.combine(a, a1)
// }
// }

implicit val int: Monoid[Int] = instance(0, _ + _)
implicit val str: Monoid[String] = instance("", _ + _)
}

import Semigroup.syntax._

"2 + 3" should "be 5" in {
2.combine(3)(Monoid.int) should be (5)
}

}

0 comments on commit 7b9c604

Please sign in to comment.