I want to declare a class like this:
class StringSetCreate(val s: String*) {
// ...
}
and call that in Java. The problem is that the constructor is of type
public StringSetCreate(scala.collection.Seq)
So in java, you need to fiddle around with the scala sequences which is ugly.
I know that there is the #annotation.varargs annotation which, if used on a method, generates a second method which takes the java varargs.
This annotation does not work on constructors, at least I don't know where to put it. I found a Scala Issue SI-8383 which reports this problem. As far as I understand there is no solution currently. Is this right? Are there any workarounds? Can I somehow define that second constructor by hand?
The bug is already filed as https://issues.scala-lang.org/browse/SI-8383 .
For a workaround I'd recommend using a factory method (perhaps on the companion object), where #varargs should work:
object StringSetCreate {
#varargs def build(s: String*) = new StringSetCreate(s: _*)
}
Then in Java you call StringSetCreate.build("a", "b") rather than using new.
Related
I'm new to Scala and in Scala I found that one can write println directly inside a class despite it not being a variable or method, how is this possible?
Even if println is a method then why aren't we defining it in the class as required generally for methods? Why despite being a method it is not allowed directly in Java class but allowed in Scala?
E.g.
class Hero {
println("running fine...") // Why is this println allowed in scala when its not inside a function?
}
object MainObject{
def main(args: Array[String]){
new Hero()
}
}
Basically, it runs inside the Class constructor. Any code inside the Class besides the variable and function declarations is constructor's code.
You can write any statement directly inside a class and it will be run whenever the class is instantiated.
When you open up an editor and start typing code, you will find, that some things are already available and some need to be imported. So all of these things which are available without importing are present in the scala.Predef package.
println is one of the methods made available by the scala compiler in the scala.Predef package.
def println() = Console.println()
If you check the scala docs, println method internally calls the Console object to print the contents on the screen.
I'm new to Scala and in Scala I found that one can write println directly inside a class despite it not being a variable or method, how is this possible?
You are wrong: println is a method, that's why this is possible. Here is the documentation for scala.Predef.println.
Consider the following example Scala class and unit test:
class BrokenClass(s: String) {
private val len = s.length
def length(): Int = len
}
class BrokenTest extends FlatSpec with Matchers with MockFactory {
"A BrokenClass" should "stub correctly" in {
val stubThing = stub[BrokenClass]
(stubThing.length _) when () returns (10)
stubThing.length should equal (10)
}
}
In older versions of ScalaMock, this code would work. With Scala 2.12 and ScalaMock 3.6, I'm getting a NullPointerException because even though I'm creating a stub, it's still invoking the "s.length" line of the constructor of BrokenClass. So it's trying to dereference "s", which is null because I haven't passed anything to it because all I want is a stub that returns a specific value when a specific method is called.
Is there a way to create a stub without it trying to invoke the object's constructor? Why did this work in older versions?
ScalaMock generates subclasses using a macro definition.
That macro gets expanded/evaluated during the compiler run.
As mocks are subclasses, the constructors of the superclasses will be called - no exceptions.
You might be able to work around this using some cglib sorcery, but that is not something i am familiar with.
So this may have been possible in older ScalaMock versions but this feature is not coming back anytime soon with the current implementation.
another option is to actually subclass this thing yourself and mock the subclass
class NotSoBrokenClass extends BrokenClass("")
...
val nsb = mock[NotSoBrokenClass]
...
That works in some cases, but if the constructor depends on non-final method calls you'll see funny behaviour (e.g. NPEs) too.
I have a class that may take from 1 to 4 parameters. They are always Strings. I would like to create an object of this class based on the number of arguments passed to the function. Is there any way to go around having to create constructor and passing an array of Objects directly to newInstance?
NewInstanceWithReflection clazz = (NewInstanceWithReflection)Class.forName("NewInstanceWithReflection").newInstance();
Constructor constructor = NewInstanceWithReflection.class.getDeclaredConstructor( new Class[] {String.class});
NewInstanceWithReflection object1 = (NewInstanceWithReflection)clazz.newInstance(new Object[]{"StackOverFlow"});
This code pasted into sbt interpreter does not seem to work. Any help appreciated.
You got it all wrong (not to mention, it's java syntax, not scala).
Something like this should work in scala:
classOf[NewInstanceWithReflection]
.getDeclaredConstructor(classOf[String])
.newInstance("StackOverFlow")
And this is what you'd need in java:
NewInstanceWithReflection
.class
.getDeclaredConstructor(String.class)
.newInstance("StackOverFlow")
I have tens of JSON fragments to parse, and for each one I need to get an instance of the right parser. My idea was to create a config file where to write the name of the class to instantiate for each parser (a kind of map url -> parser) . Getting back to your solution, I cannot call the method I implemented in each parser if I have a pointer to Any. I suppose this is a very common problem with a well-set solution, but I have no idea what the best practices could be.
I really have no experience with Java, Reflection, Class Loading and all that stuff. So,
can anyone write for me the body of the method below? I need to get an instance of a class passed as String (no arguments needed for the constructor, at least so far...)
def createInstance(clazzName: String) = {
// get the Class for the given clazzName, e.g. "net.my.BeautifulClazz"
// instantiate an object and return it
}
Thanks, as usual...
There is a very simple answer:
scala> def createInstance(clazzName: String) = Class.forName(clazzName).newInstance
createInstance: (clazzName: String)Any
scala> createInstance("java.lang.String")
res0: Any = ""
If it works for you, everything is fine. If it don't, we have to look into your class loader. This is usually the point when things will get dirty.
Depending in what you want to do, look into:
The cake pattern, if you want to combine your classes during compile time
OSGi when you want to build a plugin infrastructure (look here for a very simple example)
Google guice, if you really need dependency injection (e.g. when mixing Scala and Java code) and the cake pattern does not work for you
I'm rather new to Scala, and I am trying to use lift-squeryl-record in Lift. Scala is 2.8.1 and Lift is 2.3. My problem is that I wanted to use (Mega)ProtoUser from Record, but it conflicts with lift-squeryl-record.
I followed the instruction of:
lift-squeryl-record example
which did not use ProtoUser, and tried to define my user like this:
trait AbstractUser[MyType <: AbstractUser[MyType]] extends
ProtoUser[MyType] with Record[MyType] with KeyedRecord[Long] {
NB: KeyedRecord is from package net.liftweb.squerylrecord, not net.liftweb.record
Then I get the following error:
overriding lazy value id in trait ProtoUser of type net.liftweb.record.field.LongField[MyType]; method id in trait KeyedRecord of type => Long needsoverride' modifier`
Because both KeyedRecord and ProtoUser define a differing id method. Since I do not control the code of neither classes/traits, is there any "Scala" way around it, like renaming one of the methods? I really don't want to have to choose between the two. :(
No you cannot rename methods in a subclass. If there are two conflicting method signatures from parent types, you will need to resort to another pattern, such as indirection through delegation ( http://en.wikipedia.org/wiki/Delegation_pattern )
trait AbstractUser[MyType <: AbstractUser[MyType]] extends ProtoUser[MyType] {
def record: Record[MyType] with KeyedRecord[Long]
}