Announcing template.scala: C++ Flavored Template Metaprogramming in Scala
Hi, all,
I just published a new library template.scala, designed to create whitebox inline functions, providing ability similar to C++ template.
The template.scala library is similar to SIP-28 inline except template.scala provides whiteboxity.
Usage
scalaVersion :="2.12.1"// or "2.11.8"
resolvers +="Sonatype" at "https://oss.sonatype.org/content/groups/public"
libraryDependencies +="com.thoughtworks.template"%%"template"%"latest.release"%Provided
addCompilerPlugin("org.scalameta"%"paradise"%"3.0.0-M7" cross CrossVersion.patch)
A template function is created with a @template annotation.
@template
defmax(x: Any, y: Any) = {
if (x > y) x else y
}
Unlike normal functions, a template function will not be type-checked until using it. Thus it does not raise a type error on x > y because the real types of x and y have not been determinated.
vali:Int= max(1, 2)
vald:Double= max(8.0, 0.5)
The max function will be type-checkd and inlined whenever being invoked.
If the type of x does not support > method, it does not compile:
vals:Symbol= max('foo, 'bar)
<macro>:1: value > is not a member of Symbol
def max(x: Any, y: Any) = if (x > y) x else y
^
Recursive template functions
Template functions can be recursive, as long as the number of calls are finite and can be determined at compile-time.
@template functions always inline, never sharing similar implementations like C++ templates.
@template functions do not support type parameters.
Recursive @template functions must be resolved at compile-time.
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.