Skip to content

Instantly share code, notes, and snippets.

@NightRa
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save NightRa/9415625 to your computer and use it in GitHub Desktop.

Select an option

Save NightRa/9415625 to your computer and use it in GitHub Desktop.

Revisions

  1. NightRa renamed this gist Mar 7, 2014. 1 changed file with 3 additions and 10 deletions.
    13 changes: 3 additions & 10 deletions gistfile1.txt → decorator.scala
    Original file line number Diff line number Diff line change
    @@ -1,22 +1,15 @@
    trait Decorable[A, B <: Decorator] {
    trait Decorable[A, B] {
    def decorate(obj: A, dec: B): Either[String, A]
    }

    object Decorator {
    def decorate[A, B <: Decorator](obj: A, dec: B)(implicit d: Decorable[A,B]) = {
    d.decorate(obj, dec)
    }
    }

    abstract class Decorator {

    def decorate[A, B](implicit d: Decorable[A,B]) = d
    }


    object TagsDecorator {

    implicit object DecorableClick extends Decorable[Click, TagsDecorator] {
    override def decorate[B <: TagsDecorator](obj: Click, dec: B) = {
    override def decorate(obj: Click, dec: TagsDecorator) = {
    Try(Right(obj.copy(tags = dec.getTagsForCid(obj.dst_cid)))).recover {
    case e: Throwable => Left(e.getMessage)
    }.get
  2. Matt Muscari created this gist Mar 7, 2014.
    48 changes: 48 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    trait Decorable[A, B <: Decorator] {
    def decorate(obj: A, dec: B): Either[String, A]
    }

    object Decorator {
    def decorate[A, B <: Decorator](obj: A, dec: B)(implicit d: Decorable[A,B]) = {
    d.decorate(obj, dec)
    }
    }

    abstract class Decorator {

    }


    object TagsDecorator {

    implicit object DecorableClick extends Decorable[Click, TagsDecorator] {
    override def decorate[B <: TagsDecorator](obj: Click, dec: B) = {
    Try(Right(obj.copy(tags = dec.getTagsForCid(obj.dst_cid)))).recover {
    case e: Throwable => Left(e.getMessage)
    }.get
    }
    }

    }



    class TagsDecorator extends Decorator {

    def getTagsForCid(cid: Long): List[String] = {
    List("f")
    }

    }

    case class A {}


    object Main {

    val b: TagsDecorator = new TagsDecorator

    import TagsDecorator._


    }