Skip to content

Instantly share code, notes, and snippets.

@JohnReedLOL
Created July 4, 2016 17:57
Show Gist options
  • Save JohnReedLOL/e883c70c6dc95080f54bb9777587e558 to your computer and use it in GitHub Desktop.
Save JohnReedLOL/e883c70c6dc95080f54bb9777587e558 to your computer and use it in GitHub Desktop.
// I'm trying to understand this code...
import shapeless._
import shapeless.PolyDefns._
import nat._
import poly._
import syntax.std.traversable._
import syntax.std.tuple._
import syntax.typeable._
val arg: (Int) => Int = (x: Int) => x*2
val arg2: (PolyDefns.type, (Int) => Int) = shapeless.PolyDefns.->[(Int) => Int](arg)
object double extends shapeless.PolyDefns.->[Int, Int](arg)
object frob extends Poly1 {
implicit def caseInt = at[Int](_*2)
implicit def caseString = at[String]("!"+_+"!")
implicit def caseBoolean = at[Boolean](!_)
}
// desugar with -Xprint:typer...
/*
import shapeless._;
import shapeless.PolyDefns._;
import shapeless.`package`.nat._;
import shapeless.`package`.poly._;
import shapeless.syntax.std.traversable._;
import shapeless.syntax.std.tuple._;
import shapeless.syntax.typeable._;
val arg: Int => Int = ((x: Int) => x.*(2));
val arg2: (shapeless.PolyDefns.type, Int => Int) = scala.this.Predef.ArrowAssoc[shapeless.PolyDefns.type](shapeless.PolyDefns).->[Int => Int](arg);
object double extends shapeless.PolyDefns.->[Int,Int] {
def <init>(): double.type = {
double.super.<init>(arg);
()
}
};
object frob extends AnyRef with shapeless.Poly1 {
def <init>(): frob.type = {
frob.super.<init>();
()
};
implicit def caseInt: my.pkg.Main.frob.Case[Int]{type Result = Int} = frob.this.at[Int].apply[Int](((x$6: Int) => x$6.*(2)));
implicit def caseString: my.pkg.Main.frob.Case[String]{type Result = String} = frob.this.at[String].apply[String](((x$7: String) => "!".+(x$7).+("!")));
implicit def caseBoolean: my.pkg.Main.frob.Case[Boolean]{type Result = Boolean} = frob.this.at[Boolean].apply[Boolean](((x$8: Boolean) => x$8.unary_!))
};
*/
Here's the thing. I'm syntactically confused. For example, this compiles...
object double extends shapeless.PolyDefns.->[Int, Int](arg)
and this compiles...
object double extends shapeless.PolyDefns.->[Int, Int](arg) {
}
but this does not...
object double extends shapeless.PolyDefns.->[Int, Int]{arg} // doesn't compile
val thingThatIExtended = shapeless.PolyDefns.->[Int, Int](arg) // doesn't compile
I have never seen an object constructed this way before. What is going on?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment