I'm trying out Java 8 Date Time Api java.time using Scala REPL. Just encountered the problem below:
I do understand that the keyword with is reserved in scala, but also it is used in the API. Any idea on how to mitigate the limits?
Try wrapping the with with tick marks as follows:
val nextWed = today.`with`(java.time.temporal.TemporalAdjusters.next(DayOfWeek.WEDNESDAY))
Adding the ticks designates it a literal identifier.
Related
Future versions of Scala may use then as a keyword, as stated in SIP-12.
The compiler already shows a warning for it:
Usage of then as identifier is deprecated. It can be used as a keyword in future versions of scala.
Word then is reserved as a possible keyword in future versions of scala. It’s usage as identifier is deprecated. See SIP-12.
I'm using Mockito and have many occurrences of then methods called.
BDDMockito.then(entityService).should(Mockito.times(2)).findBy(any)
Does anyone know if there's an alternative to replace it?
In scala, wrapping any identifier in backticks "`" prevents it from being treated as a keyword.
BDDMockito.`then`(entityService).should(Mockito.times(2)).findBy(any)
This has most commonly been used to allow fields/variables/methods to be named type, but it should work for then as well. It's also sometimes used to embed spaces into identifiers.
case class Bar(`class`: Int, `type`: String) {
def `class with type`: String = s"${`class`}_${`type`}"
}
Bar(42, "skidoo").`class with type` == "42_skidoo"
Have you tried mockito-scala? its API is tailored for Scala and it caters for all the language features that the Java version doesn't
I'm experimenting with calcite from scala, and trying to pass a simple scala class for creating a schema at runtime (using ReflectiveSchema), I'm having some headache.
For example, re-implementing the FoodMart JDBC Example (which works well in Java), I'm calling it as simple as new ReflectiveSchema(new Hr()), using a Hr class rewritten in scala as:
class HR {
val emps: Array[Employee] = Array(new Employee(100, "Bill"))
}
I'm experiencing an error: ...SqlValidatorException: Object 'emps' not found within 'hr'. This problem seems to be related to the fact that val fields are actually created private in bytecode from java, and the implementation in calcite seems to be able to use (by means of java reflection) only fields accessible through the .getFields() method of a class.
So I suppose this direction requires a lot more hacking than a simple my_field.setAccessible(true) or similar.
Are there any other way to construct a schema by API, avoiding reflection and the usage of JSON?
thanks in advance for any suggestion
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
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.
In a response to:
Generating a class from string and instantiating it in Scala 2.10
#EugeneBurmako states:
"The example uses manual AST assembly, but it's possible to write a function that parses a string, finds out unbound identifiers, looks up values for them in some mapping and then creates corresponding free terms. There's no such function in Scala 2.10.0 though."
What API calls might help one find unbound identifiers?