scala-2.10

Why blocking on future considered a bad practice?

此生再无相见时 提交于 2021-02-07 14:28:30
问题 I'm trying to understand rational behind the statement For cases where blocking is absolutely necessary, futures can be blocked on (although it is discouraged) Idea behind ForkJoinPool is to join processes which is blocking operation, and this is main implementation of executor context for futures and actors. It should be effective for blocking joins. I wrote small benchmark and seems like old style futures(scala 2.9) are 2 times faster in this very simple scenario. @inline def futureResult[T

Type aliases screw up type tags?

∥☆過路亽.° 提交于 2021-01-28 03:52:38
问题 Why don't type tags work with type aliases. E.g. given trait Foo object Bar { def apply[A](implicit tpe: reflect.runtime.universe.TypeTag[A]): Bar[A] = ??? } trait Bar[A] I would like to use an alias within the following method, because I need to type A around two dozen times: def test { type A = Foo implicit val fooTpe = reflect.runtime.universe.typeOf[A] // no funciona Bar[A] // no funciona } Next try: def test { type A = Foo implicit val fooTpe = reflect.runtime.universe.typeOf[Foo] // ok

Why doesn't Scala's implicit class work when one of the type parameters should be Nothing?

蹲街弑〆低调 提交于 2020-07-28 13:54:48
问题 Update: I modified the example so that can be compiled and tested. I have an implicit class that defines an enrichment method: case class Pipe[-I,+O,+R](f: I => (O, R)); object Pipe { // The problematic implicit class: implicit class PipeEnrich[I,O,R](val pipe: Pipe[I,O,R]) extends AnyVal { def >->[X](that: Pipe[O,X,R]): Pipe[I,X,R] = Pipe.fuse(pipe, that); def <-<[X](that: Pipe[X,I,R]): Pipe[X,O,R] = Pipe.fuse(that, pipe); } def fuse[I,O,X,R](i: Pipe[I,O,R], o: Pipe[O,X,R]): Pipe[I,X,R] =

Why doesn't Scala's implicit class work when one of the type parameters should be Nothing?

∥☆過路亽.° 提交于 2020-07-28 13:53:19
问题 Update: I modified the example so that can be compiled and tested. I have an implicit class that defines an enrichment method: case class Pipe[-I,+O,+R](f: I => (O, R)); object Pipe { // The problematic implicit class: implicit class PipeEnrich[I,O,R](val pipe: Pipe[I,O,R]) extends AnyVal { def >->[X](that: Pipe[O,X,R]): Pipe[I,X,R] = Pipe.fuse(pipe, that); def <-<[X](that: Pipe[X,I,R]): Pipe[X,O,R] = Pipe.fuse(that, pipe); } def fuse[I,O,X,R](i: Pipe[I,O,R], o: Pipe[O,X,R]): Pipe[I,X,R] =

How do I resolve a short type name to a full type name in a Macro

馋奶兔 提交于 2020-01-14 01:57:59
问题 Inside a macro is there a way of using the current Context to fully expand a type name? Eg something like: context.resolveShortTypeNameToFullTypeName("Foo") = "com.acme.Foo" 回答1: Your macro might expand in a tree that includes an arbitrary import prefix.Foo , so you're asking if you can query that enclosing tree: If I emit a name Foo , how would you typecheck it? symbol.fullName is your answer. val t = c.typeCheck(q"??? : Foo").tpe.typeSymbol.fullName or use c.typecheck in 2.11. or, if you

Scala unbound placeholder parameter

我与影子孤独终老i 提交于 2020-01-13 10:27:09
问题 I am using the following code to meet my needs: (1 to 5)..map(i => s"\\x${i}") // Produces List("\\x1", "\\x2", "\\x3", "\\x4", "\\x5") But I would like to use a placeholder. According to the string interpolator documentation : (1 to 5).map(s"\\x${_}") should expand to: (1 to 5).map(StringContext("\\\\x","").s(_)) But the latter works, not the former, which throws an error: unbound placeholder parameter on _ . Why? 回答1: I believe with the syntax: (1 to 5).map(s"\\x${_}") The compiler believes

How do I format a string with string interpolation in Scala as a fixed width string?

匆匆过客 提交于 2020-01-11 01:26:51
问题 I'm interfacing with a really old system and the file I need to generate needs a field that is a formed from a string but needs to be exactly 15 in width. I want something like this: val companyName = "FooBar, Inc" // 11 chars f"$companyName%s" To return: " FooBar, Inc" Is there a slick way to do what I'm trying to do with the String interpolation? 回答1: Use String.format with a format string. Surely something there will do what you want :-) This code would do what you want: scala> val

Threading `Try`s through for-comprehension

十年热恋 提交于 2020-01-03 16:45:54
问题 Triggered by another question (which has been subsequently edited away though), I wanted to try out how easy it would be to chain calls to Scala 2.10's Try construct (cf. this presentation), using for-comprehensions. The idea is to have a list of tokens and match them against a sequence of patterns, then return the first error or the successfully matched pattern. I arrived at the following pretty awkward version, and I wonder if this can be made simpler and nicer: import util.Try trait Token

Threading `Try`s through for-comprehension

两盒软妹~` 提交于 2020-01-03 16:44:16
问题 Triggered by another question (which has been subsequently edited away though), I wanted to try out how easy it would be to chain calls to Scala 2.10's Try construct (cf. this presentation), using for-comprehensions. The idea is to have a list of tokens and match them against a sequence of patterns, then return the first error or the successfully matched pattern. I arrived at the following pretty awkward version, and I wonder if this can be made simpler and nicer: import util.Try trait Token

Scala 2.10, Double.isNaN, and boxing

邮差的信 提交于 2020-01-03 15:22:07
问题 In Scala 2.10, is someDouble.isNaN expected to box? Running my code calling .isNaN through a decompiler, I still see telltale calls to double2Double in my code. Given the new AnyVal work in 2.10, I'd expect it to be no worse than java.lang.Double.isNaN(someDouble) at runtime with no spurious allocations. Am I missing something? 回答1: Unfortunately, isNaN is a method on java.lang.Double , and it is essential to have an implicit conversion to java.lang.Double , so the Scala RichDouble value