Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
`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>
@indiescripter

This comment has been minimized.

Copy link

@indiescripter indiescripter 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

This comment has been minimized.

Copy link
Owner Author

@propensive propensive commented Mar 22, 2015

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

This comment has been minimized.

Copy link

@ivanopagano ivanopagano commented Mar 26, 2015

Is this another macro?

@propensive

This comment has been minimized.

Copy link
Owner Author

@propensive propensive commented Mar 26, 2015

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