I am trying to set an empty value (as a first choice) in a lift select element:
SHtml.select(("", "") :: (MyObject.findAll(By(MyObject.creator, User.currentUser.open_!.id))), ...
However it gives me this error:
error: type mismatch;
found: List[(String, java.lang.Object)]
required: Seq[(String, String)]
Any ideas?
Thanks!
Try breaking it up into a few steps.
val choices:Seq[(String,String)] = ("","") :: (all the rest of your options here)
scala> ("","")::List(("c","d"), ("d","e"))
res8: List[(java.lang.String, java.lang.String)] = List((,), (c,d), (d,e))
Then use that choices var as input to the SHtml.select method. There's nothing preventing this approach from working.
Example of I82Much has problems with compiling and shows errors.
I have modified his answer and have no problems. Tested.
define variable:
val choices = List(("",""), ("S", "Storage"))
and then use it
"strType" -> SHtml.select(choices, Empty, storage.strType(_), "class" -> "smallInputSelect"),
Related
The code fragment,
var line = Seq.empty[(String,Integer)]
var fileCount : Int = -1 // good definition
if (etc)
fileCount = tContSmry.getFileCount().toInt // good cast
line :+= ("etc", fileCount) // where the error??
has this error:
error: type mismatch;
found : Seq[(String, Any)]
required: Seq[(String, Integer)]
PS: using Spark version 2.2.0.2.6.4.0-91, Scala version 2.11.8
Int is not Integer. You have line: Seq[(String, Integer)], but you're trying to add ("etc", fileCount): (String, Int) to it. The error message is a little weird, I'll give you that. Integer should almost never appear in your code; you should replace it with Int.
var line = Seq.empty[(String, Int)]
// side note: don't need a var here
val fileCount: Int = if(etc) tContSmry.getFileCount().toInt else -1
line :+= ("etc", fileCount)
I assigned the variable already, but I still get the error. I think that is not a typo.
val inputJPG = input.filter(context => context.contains("jpg")).collect
inputJPG.take(10)
------------------------------------------------------
scala> inputJPG.take(10)
<console>:20: error: not found: value inputJPG
inputJPG.take(10)
Just remove collect. (except you have a reason that is not clear by your question)
val input = Seq("none","image.jpg")
val inputJPG = input.filter(context => context.contains("jpg"))
inputJPG.take(10) // -> List(image.jpg)
collect is used when you want to filter and map in one step. See the API
Scala create the Map from an list of Option emelents?
myMap = (0 to r.numRows - 1).map { i =>
if (Option(r.getValue(i,"Name")).isDefined)
(r.getValue(i, "Name")) -> (r.getValue(i, "isAvailable").toString)
}.toMap
foo(myMap) //here At this point, I'm geting the exception
I have tried above code but not compiling:
Exception:
Error:(158, 23) Cannot prove that Any <:< (T, U).
}.toMap
^
Maybe try this code:
val myMap = (0 until r.numRows) flatMap { i =>
for {
name <- Option(r.getValue(i, "Name"))
available = r.getValue(i, "isAvailable").toString
} yield (name, available)
}.toMap
foo(myMap)
Your problem is most likely that you use if without else, and if is an expression in scala, which means it evaluates to something.
if (2 > 3) true
is equivalent to
if (2 > 3) true else ()
so the type is the common supertype of Unit and Boolean which is Any and this is the error you get.
Note that you can replace to and -1 with until which does the same thing but is more readable.
You shouldn't really check Option with isDefined and perform action on result if that's true, to do this you use map operation.
To explain my implementation a little: the for part will evaluate to Option[(String, String)], and will contain your tuple if "Name" key was present, otherways None. Conceptually, flatMap will first change your range of indices to a sequence of Option[(String, String)], and then flatten it, i.e. remove all Nones and unwrap all Somes.
If you want to check "isAvailable" for null as well, you can do similar thing
for {
name <- Option(r.getValue(i, "Name"))
available <- Option(r.getValue(i, "isAvailable"))
} yield (name, available.toString)
I have a form where there is a number expected... I'm having a hard time verifying whether it has been submitted or not to return a message saying that it's required, I tried the following cases but none worked:
"orderBy" -> number.verifying("The order is required",_.isInstanceOf[Int])
"orderBy" -> number.verifying("The order is required",_>0)
Any ideas?
If orderBy isn't submitted with the Form at all, then it will return a FormError with the key error.required.
I assume you mean the case when an empty String is submitted instead of number. The problem with your attempts is that the verifying functions are never reached, because the empty String doesn't make it past the number validator.
The only thing I can think of is making a custom Mapping[Int] that first checks to see if the field is empty, then checks to see if it's a valid Int.
val requiredNumber: Mapping[Int] = Forms.nonEmptyText
.verifying("Must be numeric", i => Try(i.toInt).isSuccess || i.isEmpty)
.transform[Int](_.toInt, _.toString)
And testing:
scala> val form = Form(mapping("orderBy" -> requiredNumber)(identity)(Some(_)))
scala> form.bind(Map("orderBy" -> "1")).value
res24: Option[Int] = Some(1)
scala> form.bind(Map("orderBy" -> "")).errors
res26: Seq[play.api.data.FormError] = List(FormError(orderBy,List(error.required),WrappedArray()))
scala> form.bind(Map("orderBy" -> "aa")).errors
res27: Seq[play.api.data.FormError] = List(FormError(orderBy,List(Must be numeric),WrappedArray()))
scala> form.bind(Map("orderByzzz" -> "2")).errors
res28: Seq[play.api.data.FormError] = List(FormError(orderBy,List(error.required),List()))
Look at the following Map:
scala> val v = Map("id" -> ("_id", "$oid")).withDefault(identity)
v: scala.collection.immutable.Map[String,java.io.Serializable] = Map(id -> (_id,$oid))
The compiler generates a Map[String,java.io.Serializable] and the value of id can be retrieved like this:
scala> v("id")
res37: java.io.Serializable = (_id,$oid)
Now, if I try to access an element that does not exist like this...
scala> v("idx")
res45: java.io.Serializable = idx
... then as expected I get back the key itself... but how do I get back a tuple with the key itself and an empty string like this?
scala> v("idx")
resXX: java.io.Serializable = (idx,"")
I always need to get back a tuple, regardless of whether or not the element exists.
Thanks.
Instead of .withDefault(identity) you can use
val v = Map("id" -> ("_id", "$oid")).withDefault(x => (x, ""))
withDefault takes as a parameter a function that will create the default value when needed.
This will also change the return type from useless Serializable to more useful (String, String).