Skip to content

Instantly share code, notes, and snippets.

@propensive
Created March 21, 2015 22:34
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save propensive/adcc36edd145ff7d8371 to your computer and use it in GitHub Desktop.
Save propensive/adcc36edd145ff7d8371 to your computer and use it in GitHub Desktop.
`alloc`, an alternative to `new`, with better syntax and type inference
Welcome to Scala version 2.10.5 (OpenJDK 64-Bit Server VM, Java 1.6.0_27).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import rapture.core._
import rapture.core._
scala> val str: String = alloc() // new String()
str: String = ""
scala> val date: java.util.Date = alloc() // new java.util.Date()
date: java.util.Date = Sat Mar 21 23:33:20 CET 2015
scala> val sb = alloc[StringBuilder]() // new StringBuilder()
sb: StringBuilder =
scala> val exc: RuntimeException = alloc("Error message") // new RuntimeException("Error message")
exc: RuntimeException = java.lang.RuntimeException: Error message
scala> case class Tc[T](val default: T)
defined class Tc
// Particularly useful for removing repetition in an implicit's type and constructor call
scala> implicit val stringTc: Tc[String] = alloc("foo")
stringTc: Tc[String] = Tc@5e4d273a
scala>
Copy link

ghost commented Mar 22, 2015

Just downloaded rapture.core to try out your new allow alternative for new

Got compile problem

not found: value intersectionType macros.scala /rapture-core-master/src/rapture/core line 77

Scala Problem

val intersection = exceptionTypes.filter(_ != weakTypeOf[E2])
val resultType = intersection.length match {
case 0 => typeOf[Nothing]
case 1 => intersection.head
case _ => intersectionType(intersection) //this line gives error above
}

Regards Justin Johansson

@propensive
Copy link
Author

Looks strange to me... how are you compiling it? If you're using my working copy in source, you'll need to use Scala 2.10 (not 2.11).

But something else is clearly broken, because when I try and compile it using SBT, I get a different error saying "not found: value implicitly", which makes no sense...

Have I ever mentioned how much I hate build systems?! ;)

@ivanopagano
Copy link

Is this another macro?

@propensive
Copy link
Author

Yes, it's implemented with a macro.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment