scala-2.8

How do I idiomatically handle null checks from within Scala/Lift?

爱⌒轻易说出口 提交于 2020-01-29 07:02:31
问题 Even with the prevalence of the Box and Option monads, we still have to check for null values here and there. The best I've come up with so far is by using the Box#!! method: (Box !! possiblyNull).map(_.toString).openOr("") Is there a better way to do this? I tried using Box's apply method: Box(possiblyNull).map(_.toString).openOr("") But the compiler complained of an ambiguous reference to an overloaded definition, specifically: [InType,OutType](value: InType) (pf: PartialFunction[InType

How do I idiomatically handle null checks from within Scala/Lift?

好久不见. 提交于 2020-01-29 07:00:12
问题 Even with the prevalence of the Box and Option monads, we still have to check for null values here and there. The best I've come up with so far is by using the Box#!! method: (Box !! possiblyNull).map(_.toString).openOr("") Is there a better way to do this? I tried using Box's apply method: Box(possiblyNull).map(_.toString).openOr("") But the compiler complained of an ambiguous reference to an overloaded definition, specifically: [InType,OutType](value: InType) (pf: PartialFunction[InType

What is the proper way to remove elements from a scala mutable map using a predicate

蹲街弑〆低调 提交于 2020-01-22 13:27:52
问题 How to do that without creating any new collections? Is there something better than this? val m = scala.collection.mutable.Map[String, Long]("1" -> 1, "2" -> 2, "3" -> 3, "4" -> 4) m.foreach(t => if (t._2 % 2 == 0) m.remove(t._1)) println(m) P.S. in Scala 2.8 回答1: retain does what you want. In 2.7: val a = collection.mutable.Map(1->"one",2->"two",3->"three") a: scala.collection.mutable.Map[Int,java.lang.String] = Map(2 -> two, 1 -> one, 3 -> three) scala> a.retain((k,v) => v.length < 4) scala

Scala - implicit conversion of Int to Numeric[Int]

廉价感情. 提交于 2020-01-10 15:36:24
问题 I've created a class that can be parameterised by anything that can be converted to Numeric class Complex[T <% Numeric[T]] (val real : T, val imag : T) { //... complex number methods ... } Then elsewhere in the code I try: var myComplex = new Complex(0, 1) This raises a compilation error because (surprisingly) there's no implicit conversion between Int and Numeric[Int] or even between Int and Integral[Int]. Am I missing something? Is there an implicit conversion somewhere I'm not seeing?

Manifest vs ClassManifest. What does this Scala error mean?

为君一笑 提交于 2020-01-04 02:54:54
问题 What does this error mean? scala> val a = Array[{ def x: Int }](new { def x = 3 }) <console>:5: error: type mismatch; found : scala.reflect.Manifest[java.lang.Object] required: scala.reflect.ClassManifest[AnyRef{def x: Int}] val a = Array[{ def x: Int }](new { def x = 3 }) ^ I don't have a clue ... 回答1: Ok, let's consider a couple of things. First: type T = { def x: Int } This type is known as a structural type . It defines not a class, but a set of objects that share methods with a certain

scala: Adding attributes (odd and even rows) to xml table

三世轮回 提交于 2020-01-03 05:20:08
问题 In a Lift application, I’d like to add a special tag which takes the <tbody> part of the next table and adds odd and even classes (for example) to each <tr> tag. Alternating, of course. While I have found a way to add another attribute to all <tr> tags, there are still a few problems left (see code below). First, it doesn’t work. cycle.next is called too often, so in the end, everything is an odd row. Other problems are that the code doesn’t exclude inner tables (so a nested <tr> would be

Problems with Scala Swing library

两盒软妹~` 提交于 2020-01-02 12:54:30
问题 Hello I am having problems when using the Scala Swing library in version 2.8 Beta1-prerelease. I have a situation where I want to show a table in GUI, and update it as results are returned from a SQL request. Which way could this be done in Scala, at the moment i am using the DefaultTableModel from Java library. Another thing is that I want the table to be sortable afterwards, I cant see if Scala swing library supports this either? 回答1: No - the scala swing library does not support sorting of

Problems with Scala Swing library

空扰寡人 提交于 2020-01-02 12:53:45
问题 Hello I am having problems when using the Scala Swing library in version 2.8 Beta1-prerelease. I have a situation where I want to show a table in GUI, and update it as results are returned from a SQL request. Which way could this be done in Scala, at the moment i am using the DefaultTableModel from Java library. Another thing is that I want the table to be sortable afterwards, I cant see if Scala swing library supports this either? 回答1: No - the scala swing library does not support sorting of

@BeanProperty with PropertyChangeListener support?

人盡茶涼 提交于 2020-01-02 05:07:22
问题 @BeanProperty generates simple get / set methods. Is there a way to automatically generate such methods with support for firing property change events (e.g. I want to use it with JFace Databinding?) 回答1: I've had the same question, and have been keeping a close eye out for possible answers. I think I've just stumbled across one (although I haven't tried it yet). Scala 2.9 has a feature for handling dynamic calls (meant for integration with dynamic languages, I suspect). Essentially, calls to

@BeanProperty with PropertyChangeListener support?

筅森魡賤 提交于 2020-01-02 05:05:09
问题 @BeanProperty generates simple get / set methods. Is there a way to automatically generate such methods with support for firing property change events (e.g. I want to use it with JFace Databinding?) 回答1: I've had the same question, and have been keeping a close eye out for possible answers. I think I've just stumbled across one (although I haven't tried it yet). Scala 2.9 has a feature for handling dynamic calls (meant for integration with dynamic languages, I suspect). Essentially, calls to