Does JEXL API work in Scala? - scala

Assuming that foo.method() can be call in a Jexl expression on java, is this possible to happen using the same api in Scala.

Scala allows to use all Java libs, Jexl isn't an exception. So you can make a call of Jexl class methods in Scala as well.
As example:
val ja = new JexlArithmetic(false);
ja.add(null, null) // 0

Related

When would request.attrs.get in a Scala Play custom filter return None?

In an answer to "How to run filter on demand scala play framework", the following code is suggested:
// in your filter
val handlerDef: Option[HandlerDef] = request.attrs.get(Router.Attrs.HandlerDef)
I'm not sure what's happening here - is it safe to .get on this val (to get it out of the Option)? In what scenarios would this code result in a None (ie, when would Router.Attrs.HandlerDef not be present)?
I'm working with Scala and PlayFramework 2.6.
According to Route modifier tags
Please be aware that the HandlerDef request attribute exists only when
using a router generated by Play from a routes file. This attribute is
not added when the routes are defined in code, for example using the
Scala SIRD or Java RoutingDsl. In this case
request.attrs.get(HandlerDef) will return None in Scala or null in
Java. Keep this in mind when creating filters.
Hence if you are using routes file then Router.Attrs.HandlerDef should always be available. As a shorthand instead of
val handlerDef: HandlerDef = request.attrs.get(Router.Attrs.HandlerDef).get
your can use apply sugar like so
val handlerDef: HandlerDef = request.attrs(Router.Attrs.HandlerDef)

Can Scala classes be used in Java

class Wish{
val s = "Hello! User. Wish you a Great day."
}
object Wish{
def main(args: Array[String]){
val w = new Wish()
println("Value - " + w.s )
}
}
Java classes can be used in Scala. Similarly, can Scala classes be used in Java?
Yes, Scala classes can be called from Java and vice versa.
The below text is taken from: Scala FAQs
What does it mean that Scala is compatible with Java?
The standard Scala backend is a Java VM. Scala classes are Java classes, and vice versa. You can call the methods of either language from methods in the other one. You can extend Java classes in Scala, and vice versa. The main limitation is that some Scala features do not have equivalents in Java, for example traits.
The following post also could be helpful to you: how to call Scala from Java
Yes. If you want to do this, there are a few things you might want to remember:
Do not use operators in your method names or provide a wordy alternative. Operator names can be called from Java but are mangled into somethings very ugly.
Java users might expect Java style getters and setters. You can produce those automatically by adding #BeanProperty annotation to fields.
In the same way Java user might be accustomed to factory methods called ClassName.of where Scala uses .apply. Those you have to provide by hand, if you want to provide that service.

Calling `getBudgetId` in the Google Adwords API from Scala

In Google Adwords API, this is the Java code to get the budget id:
Long budgetId = budgetService.mutate(new BudgetOperation[] {budgetOperation}).getValue(0).getBudgetId();
I need to convert this to Scala code as I am working with Google AdWords API in Scala.
The transformation to Scala is pretty straightforward: immutable variables start with val; you can drop the type specification (it's inferred), and instantiating arrays is not a special case, but is just like instantiating a List or Map, etc.
val budgetId = budgetService.mutate(Array(budgetOperation)).getValue(0).getBudgetId()
The IntelliJ Scala plugin has a feature that will attempt to automatically convert Java code to Scala. While it doesn't always get it right, it may be of aid in learning how to map Java constructs into Scala.

How to pass Collection of parameters to chaining pattern API in Scala

I am using JAVA library where its API is built on chaining pattern, like:
(new SomeClass).method1("data1").method1("data2").method2("data6")
My data is in list:
val list = List("data1","data2","data3","data4")
I want to pass them as parameters for each method1 call. My current version is:
list.foldLeft(new SomeClass)((a,b)=>a.method1(b))
Is there more appropriate or idiomatic way of doing this?

Using scala vararg methods in java

Why do all scala vararg methods, when used from java, seem to accept a Seq of variables, and can't be used as java native vararg methods. Is this a bug?
For instance, Buffer has method def append(elems: A*): Unit. But in java it has another signature: void append(Seq<A>).
If you control the scala code you can use #varargs to make it generate a java-compatible varags method, e.g. #varargs def append(elems: A*): Unit = {}
It is not a bug. It is a design choice that favors vararg use within Scala over interoperability with Java. For example, it allows you to pass a List into a Scala varargs method without having to convert it to an Array on the way.
If you need to use Scala varargs from Java, you should create some scala Seq instead. You can, for example, write a Java wrapper to get an array automatically created, and then use the genericWrapArray method from the Predef object.
you can easily cast a Seq in varargs using :_*. For example :
val b = collection.mutable.ListBuffer.empty[Int]
b.append(List(1, 2):_*)
so this avoid code duplication in the collection API.
You can also simply use appendAll :
b.appendAll((List(1, 2))