What does `T {}` do in Scala

Any type can be followed by a {} enclosed sequence of type and abstract non-type member definitions. This is known as a “refinement” and is used to provide additional precision over the base type that is being refined. In practice refinements are most commonly used to express constraints on abstract type members of the type … Read more

Using the “Prolog in Scala” to find available type class instances

This can be done (at least in some cases) with compiler internals import scala.language.experimental.macros import scala.reflect.internal.util import scala.reflect.macros.{blackbox, contexts} object Macros { def allImplicits[A]: List[String] = macro impl[A] def impl[A: c.WeakTypeTag](c: blackbox.Context): c.Tree = { import c.universe._ val context = c.asInstanceOf[contexts.Context] val global: context.universe.type = context.universe val analyzer: global.analyzer.type = global.analyzer val callsiteContext = context.callsiteTyper.context … Read more

Why is the Aux technique required for type-level computations?

There are two separate questions here: Why does Shapeless use type members instead of type parameters in some cases in some type classes? Why does Shapeless include Aux type aliases in the companion objects of these type classes? I’ll start with the second question because the answer is more straightforward: the Aux type aliases are … Read more

Testing an assertion that something must not compile

Not a framework, but Jorge Ortiz (@JorgeO) mentioned some utilities he added to the tests for Foursquare’s Rogue library at NEScala in 2012 which support tests for non-compilation: you can find examples here. I’ve been meaning to add something like this to shapeless for quite a while. More recently, Roland Kuhn (@rolandkuhn) has added a … Read more

Can someone explain to me what the Shapeless library is for? [closed]

It’s a little hard to explain, as shapeless has a wide range of features; I’d probably find it easier to “explain, in simple terms, what variables are for”. You definitely want to start with the feature overview. Broadly speaking, shapeless is about programming with types. Doing things at compile-time that would more commonly be done … Read more

Any reason why scala does not explicitly support dependent types?

Syntactic convenience aside, the combination of singleton types, path-dependent types and implicit values means that Scala has surprisingly good support for dependent typing, as I’ve tried to demonstrate in shapeless. Scala’s intrinsic support for dependent types is via path-dependent types. These allow a type to depend on a selector path through an object- (ie. value-) … Read more

Limits of Nat type in Shapeless

I will attempt one myself. I will gladly accept a better answer from Travis Brown or Miles Sabin. Nat can currently not be used to represent large numbers In the current implementation of Nat, the value corresponds to the number of nested shapeless.Succ[] types: scala> Nat(3) res10: shapeless.Succ[shapeless.Succ[shapeless.Succ[shapeless._0]]] = Succ() So to represent the number … Read more