Skip to content

Instantly share code, notes, and snippets.

@paulp
Created April 25, 2014 21:19
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save paulp/11303639 to your computer and use it in GitHub Desktop.
Save paulp/11303639 to your computer and use it in GitHub Desktop.
// Quasiquoted excerpt
def cdef = q"""
class $ClassName[..$classTypeParams](..$primaryParams) extends ..$classParents {
..$primaryAccessors
def get = this
def isEmpty = ${quasi.isEmpty}
def copy(..$primaryWithDefaults) = $ObjectName(..$primaryNames)
override def productPrefix: String = $ClassNameString
override def productIterator: $primaryIteratorType = Iterator(..$primaryNames)
override def canEqual(that: Any): Boolean = ${CanEqualLogic("that")}
override def toString(): String = $ToStringLogic
override def hashCode(): Int = $HashCodeLogic
override def equals(that: Any): Boolean = ${EqualsLogic("that")}
}"""
def mdef = q"""
object $ObjectName extends $objectParent {
private[this] val $NoClassName: $ClassType = ${quasi.createEmpty()}
override final def toString = $ClassNameString
def apply[..$classTypeParams](..$primaryParams): $ClassName = $newClassType
def unapply[..$classTypeParams](x: $ClassType): $ClassType = if (x eq null) $NoClassName else x
private def readResolve(): Object = $ObjectName
}"""
// Generating
QuasiCaseClass("Rational").vparam[Int]("n").vparam[Int]("d").result
class Rational(val n: Int, val d: Int) extends AnyRef with Product2[Int, Int] {
def _1 = n;
def _2 = d;
def get = this;
def isEmpty = d == 0;
def copy(n: Int = n, d: Int = d) = Rational(n, d);
override def productPrefix: String = "Rational";
override def productIterator: Iterator[Int] = Iterator(n, d);
override def canEqual(that: Any): Boolean = that.isInstanceOf[Rational];
override def toString(): String = List(n, d).mkString("Rational".+("("), ", ", ")");
override def hashCode(): Int = {
var acc: Int = -889275714;
scala.runtime.Statics.mix(acc, n);
scala.runtime.Statics.mix(acc, d);
scala.runtime.Statics.finalizeHash(acc, 2)
};
override def equals(that: Any): Boolean = this.eq(that.asInstanceOf[AnyRef]).||(true)
}
object Rational extends _root_.scala.Function2[Int, Int, Rational] {
private[this] val NoRational: Rational = new Rational(0, 0);
final override def toString = "Rational";
def apply(n: Int, d: Int): Rational = new Rational(n, d);
def unapply(x: Rational): Rational = if (x.eq(null))
NoRational
else
x;
private def readResolve(): Object = Rational
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment