scala-2.10

converting Akka's Future[A] to Future[Either[Exception,A]]

a 夏天 提交于 2019-11-29 14:40:55
问题 Is there a method in Akka (or in the standard library in Scala 2.10) to convert a Future[A] which might fail into a Future[Either[Exception,A]] ? I know that you can write f.map(Right(_)).recover { case e:Exception => Left(e) } It just seems to be such a common task that I wonder whether I have overlooked something. I'm interested in answers for Scala 2.9/Akka and Scala 2.10. 回答1: The primary reason why this method is missing is that it does not really have good semantics: the static type

Option.fold in scala 2.10

北慕城南 提交于 2019-11-29 11:06:33
In the following session with scala 2.10.0-M7: scala> trait A defined trait A scala> class B extends A defined class B scala> class C extends A defined class C scala> Some(0).fold(new B){_=>new C} <console>:11: error: type mismatch; found : C required: B Some(0).fold(new B){_=>new C} I would expect the compiler find the common supertype (namely A) rather than complaining. Is it the general type inference limitation, or the consequence of the way Option.fold is defined? Thank you. kiritsuku The problem results of a combination of Scalas type inference algorithm and the way how Option.fold is

Scala Reflection - Loading or finding classes based on trait

纵饮孤独 提交于 2019-11-29 10:35:40
Does the scala reflection API (2.10) provide any easier means of searching the loaded classes and filtering the list to specific classes which implement a defined trait? ie; trait Widget { def turn(): Int } class Cog extends Widget { def turn() = { 5 } } class Sprocket extends Widget { def turn() = { 10 } } I want to search the class library for anything that extends Widget and instantiate those classes. So I would end up with an instance of Cog and Sprocket . I've done similar in Java iterating through the class directories, forming class names and using Class.forName to load a Class object

Scala macro to print code?

大兔子大兔子 提交于 2019-11-29 07:32:31
I want to do something like this: def assuming[A](condition: => Boolean)(f: => A): A = { require(condition, /* print source-code of condition */) f } Sample usage: def fib(n: Int) = n match { // yes, yes, I know this is not efficient case 0 => 0 case 1 => 1 case i => assuming(i > 0) { fib(i-1) + fib(i-2) } } Now, for example, if you call fib(-20) , I want it to throw an exception with a message like Assertion failed: -20 > 0 or Assertation failed: i > 0 Dude, isn't an assert macro one of the basic use cases you implement to learn how to use macros? Well, that's what I thought, too. By "glean

Custom Scala enum, most elegant version searched

风格不统一 提交于 2019-11-29 03:52:57
For a project of mine I have implemented a Enum based upon trait Enum[A] { trait Value { self: A => _values :+= this } private var _values = List.empty[A] def values = _values } sealed trait Currency extends Currency.Value object Currency extends Enum[Currency] { case object EUR extends Currency case object GBP extends Currency } from Case objects vs Enumerations in Scala . I worked quite nice, till I run into the following problem. Case objects seem to be lazy and if I use Currency.value I might actually get an empty List. It would have been possible to make a call against all Enum Values on

How to remove an item from a list in Scala having only its index?

∥☆過路亽.° 提交于 2019-11-29 01:26:38
I have a list as follows: val internalIdList: List[Int] = List() internalIdList = List(11, 12, 13, 14, 15) From this list would remove the third element in order to obtain: internalIdList = List(11, 12, 14, 15) I can not use a ListBuffer , are obliged to maintain the existing structure. How can I do? Thanks to all If you know that you will be dropping the third element (index 2), then you can simply use val trunced = internalIdList.take(2) ++ internalIdList.drop(3) otherwise, if you don't know beforehand what the index will be of the element to remove, you could write a function like the

Enabling the macro-paradise Scala compiler plugin in Maven projects

两盒软妹~` 提交于 2019-11-28 23:32:39
I have the ordinary scala-2.10 macros working in a maven project just by including the scala-reflect.jar library as a dependency in the pom, but what do I need to turn on macro-paradise? I am using scala-2.10 and scala-maven-plugin-3.1.5. Looks like I got it to work with the following additions to the pom.xml <repositories> ... <repository> <id>oss.sonatype.org</id> <name>sonatype sapshots</name> <url>https://oss.sonatype.org/content/repositories/snapshots</url> </repository> ... </repositories> and <plugins> ... <plugin> ... scala-maven-plugin identification... <configuration> ...

How do you update multiple columns using Slick Lifted Embedding?

半城伤御伤魂 提交于 2019-11-28 17:11:13
How do you update multiple columns using Slick Lifted Embedding ? This document doesn't say much. I expected it to be something like this Query(AbilitiesTable).filter((ab: AbilitiesTable.type) => ab.id === ability_id).map((ab: AbilitiesTable.type) => (ab.verb, ab.subject)).update("edit", "doc") expert I figured it out. It should be like this val map = Query(AbilitiesTable) .filter(_.id === ability_id) .map(ab => ab.verb ~ ab.context) map.update(("", "")) Typesafe , why your documentation is so bad ? I have to Google pretty much every silly thing or dig through unit-tests for hours. Please

Documenting Scala 2.10 macros [closed]

孤人 提交于 2019-11-28 15:55:05
问题 I'll start with an example. Here's an equivalent of List.fill for tuples as a macro in Scala 2.10: import scala.language.experimental.macros import scala.reflect.macros.Context object TupleExample { def fill[A](arity: Int)(a: A): Product = macro fill_impl[A] def fill_impl[A](c: Context)(arity: c.Expr[Int])(a: c.Expr[A]) = { import c.universe._ arity.tree match { case Literal(Constant(n: Int)) if n < 23 => c.Expr( Apply( Select(Ident("Tuple" + n.toString), "apply"), List.fill(n)(a.tree) ) )

Scala Constructor Parameters

五迷三道 提交于 2019-11-28 11:54:13
What is the difference between a private var constructor parameter and a constructor parameter without val/var? Are they same in terms of scope/visibility? Ex: class Person(private var firstName:String, lastName:String) Yes, there are two important differences. First for the easy one: constructor parameters without the var or val keywords are not mutable variables—their values can't be changed in the body of the class. Even if we restrict ourselves to the val keyword, though, there's still a difference between private val and keyword-less parameters. Consider the following: class Person