How do I invoke XYZ.doSomething method for these classes:
XYZ.scala
object XYZ {
private def doSomething(i : Int)(implicit s: String): String {
s + i.toString
}
}
XYZTest.scala
class XYZTest extends FunSpec with PrivateMethodTester {
describe("SomeTest") {
it("Can't find a private method named: doSomething :( ") {
implicit lazy val someStr: String = "sahil"
val doSomething = PrivateMethod[String]('doSomething)
val myStr = XYZ invokePrivate doSomething(1)
assert(myStr == "sahil1")
}
}
describe("SomeTest") {
it("This doesn't even compile :( ") {
val doSomething = PrivateMethod[String]('doSomething)
val myStr = XYZ invokePrivate doSomething(1)("sahil")
assert(myStr == "sahil1")
}
}
}
Correct answer is:
object XYZ {
private def doSomething(i : Int)(implicit s: String): String = {
s + i.toString
}
}
class XYZTest extends FunSpec with PrivateMethodTester {
describe("SomeTest") {
it("Can't find a private method named: doSomething :( ") {
implicit lazy val someStr: String = "sahil"
val doSomething = PrivateMethod[String]('doSomething)
val myStr = XYZ invokePrivate doSomething(1, someStr)
assert(myStr == "sahil1")
}
}
}
Brief view on invokePrivate method shows that it does not support implicits, but looks like it treats currying argument list as general sequence of arguments
def invokePrivate[T](invocation : PrivateMethodTester.this.Invocation[T])
where args is the sequence:
final class Invocation[T](val methodName : scala.Symbol, val args : scala.Any*)
Related
class Person {
val studentName = "Arpana"
def changeName(id:String, name:String) ={
val studentName = name
useName(id)
}
def useName(id:String) = {
println(s"use name is $id, by $studentName")
}
}
object Person {
def main(args: Array[String]): Unit = {
(new Person).changeName("2", "Shubham")
}
}
I don't want to use var in code, can we do it by keywords, I tried with keywords like super, protected, private, final but didn't work.
In actual I want to apply this in the below code.
abstract class BaseRepository[T <: BaseModel : ClassTag : StriveSerializer] {
self: BaseConnection =>
val tableName: String = implicitly[ClassTag[T]].runtimeClass.getSimpleName
private val serializer = implicitly[StriveSerializer[T]]
private def executeInserts(query: String): Future[Boolean] = Future {
val preparedStatement = self.connection.prepareStatement(query)
preparedStatement.execute()
}
def exist(id: String, name: String): Future[Boolean] = {
val tableName = name
val promise = Promise[Boolean]
queryById(id).onComplete {
case Success(_) => promise.success(true)
case Failure(ex) => promise.failure(ex)
}
promise.future
}
def queryById(id: String): Future[T] = {
val getSql = s"SELECT * FROM $tableName WHERE id == $id;"
executeReads(getSql).map(serializer.fromResultSet)
}
}
I want when i call exist function then table name given in exist function override in queryById method table name .
It seems like a bit of mix of Java and Scala style. I tried to refactor a bit assuming the intention behind the code. Try and see if this achieves what you want to do:
class Person(_id: String, _studentName: String) {
private val id: String = _id
private val studentName: String = _studentName
def useName() = {
println(s"use name is $id, by $studentName")
}
}
object Person extends App {
new Person("2", "Shubham").useName()
}
I think you should use case class For Model
case class Student(id:String,name:String)
def changeId(student:Student, newId:String): Student ={
student.copy(id=newId)
}
val s1 = Student("1","A")
val newS1 = changeId(s1,"2")
I think it okay to use mutable in a class
e.g.
class MySuperService{
var lastHeartbeat: Option[Timestamp] = None
def setLastHeartbeat(ts:Timestamp): Unit ={
lastHeartbeat = Some(ts)
}
}
val mss1 = new MySuperService()
mss1.setLastHeartbeat(???)
I have one main class like this:
class Test {
def exe(first:String, second:String, task:String):String = {
task match {
case "A" => {
val obj = new A(first)
obj.defineSecond(second)
}
case "B" => {
val obj = new B(first)
obj.defineSecond(second)
}
case "C" => {
val obj = new C(first)
obj.defineSecond(second)
}
....so many cases
}
}
}
Instead of writing case in my Test class everytime a new class is added, I tried using the concept of reflection in scala.
Below is what I trying:
val m = ru.runtimeMirror(getClass.getClassLoader)
val classTest = ru.typeOf[Test].typeSymbol.asClass
val cm = m.reflectClass(classTest)
But getting error as "class Test is inner class, use reflectClass on an InstaneMirror to obtain its classMirror".
Can anyone knows how can I can avoid adding cases to my main class everytime a new class is created, instead I can write my main class in a way it will work for every case.
I guess you haven't provided all necessary information in your question. It's written that "class Test is inner class" in your error message but Test is not inner in your code snippet. If you want your runtime-reflection code to be fixed please provide code snippet that reflects actual use case.
Meanwhile you can try a macro (working at compile time)
import scala.language.experimental.macros
import scala.reflect.macros.blackbox
class Test {
def exe(first: String, second: String, task: String): String = macro Test.exeImpl
}
object Test {
def exeImpl(c: blackbox.Context)(first: c.Tree, second: c.Tree, task: c.Tree): c.Tree = {
import c.universe._
val cases = Seq("A", "B", "C").map(name =>
cq"""${Literal(Constant(name))} => {
val obj = new ${TypeName(name)}($first)
obj.defineSecond($second)
}"""
)
q"$task match { case ..$cases }"
}
}
Usage:
class A(s: String) {
def defineSecond(s1: String): String = ""
}
class B(s: String) {
def defineSecond(s1: String): String = ""
}
class C(s: String) {
def defineSecond(s1: String): String = ""
}
new Test().exe("first", "second", "task")
//scalac: "task" match {
// case "A" => {
// val obj = new A("first");
// obj.defineSecond("second")
// }
// case "B" => {
// val obj = new B("first");
// obj.defineSecond("second")
// }
// case "C" => {
// val obj = new C("first");
// obj.defineSecond("second")
// }
//}
I want to override the val t from getValue which returns a Future
trait demo{
val t :String
}
class Test1 extends demo{
override val t = "abc"
}
class Test2 extends demo{
override val t = ""
private def getValue ={
Future(Option("abc"))
}
}
What you can do that kind of comes close to that is to map a future to an anonymous class extending demo:
val aDemo: Future[demo] = Future("abc").map { value =>
new demo {
override val t = value
}
}
Of course, you can do that inside a for-comprehension as well, e.g.:
for (value <- Future("abc")) yield
new demo {
override val t = value
}
I'm trying to use fixture-context objects with async testing in ScalaTest.
The naive approach of simply combining the two doesn't compile. For example:
import org.scalatest.AsyncWordSpec
import scala.collection.GenTraversableOnce
import scala.concurrent.{ExecutionContext, Future}
import scala.math.Numeric.IntIsIntegral
trait Adder[T] {
implicit def num: Numeric[T]
def add(number: T): Unit
def result: Future[T]
}
object Foo {
def doubleSum[T](adder: Adder[T], numbers: GenTraversableOnce[T])(implicit ec: ExecutionContext): Future[T] = {
numbers.foreach(adder.add)
val num = adder.num
import num._
adder.result.map(result => result + result)
}
}
class FooSpec extends AsyncWordSpec {
trait IntAdder {
val adder = new Adder[Int] {
override implicit val num = IntIsIntegral
private var sum = Future.successful(num.zero)
override def add(number: Int): Unit = sum = sum.map(_ + number)
override def result: Future[Int] = sum
}
}
"Testing" should {
"be productive" in new IntAdder {
Foo.doubleSum(adder, Seq(1, 2, 3)).map(sum => assert(sum == 12))
}
}
}
This fails to compile with:
Error:(37, 11) type mismatch;
found : FooSpec.this.IntAdder
required: scala.concurrent.Future[org.scalatest.compatible.Assertion]
new IntAdder {
This is a legitimate error but I'm wondering what ways there are of solving this in a ScalaTest style.
I want to keep the fixture-context object since that allows me to use the stackable trait pattern.
What about:
import org.scalatest.compatible.Assertion
class FooSpec extends AsyncWordSpec {
def withIntAdder(test: Adder[Int] => Future[Assertion]): Future[Assertion] = {
val adder = new Adder[Int] { ... }
test(adder)
}
"Testing" should {
"be productive" in withIntAdder { adder =>
Foo.doubleSum(adder, Seq(1, 2, 3)).map(sum => assert(sum == 12))
}
}
}
Or
class FooSpec extends AsyncWordSpec {
trait IntAdder {
val adder = new Adder[Int] {
override implicit val num = IntIsIntegral
private var sum = Future.successful(num.zero)
override def add(number: Int): Unit = sum = sum.map(_ + number)
override def result: Future[Int] = sum
}
}
trait SomeMoreFixture {
}
"Testing" should {
"be productive" in {
val fixture = new IntAdder with SomeMoreFixture
import fixture._
Foo.doubleSum(adder, Seq(1, 2, 3)).map(sum => assert(sum == 12))
}
}
}
The best solution I have come up with so far is to do something like:
class FooSpec extends AsyncWordSpec {
trait IntAdder {
... // Same as in the question
val assertion: Future[compatible.Assertion]
}
"Testing" should {
"be productive" in new IntAdder {
val assertion = Foo.doubleSum(adder, Seq(1, 2, 3)).map(sum => assert(sum == 12))
}.assertion
}
}
I was hoping to reduce it down slightly more into:
class FooSpec extends AsyncWordSpec {
trait IntAdder extends (() => Future[compatible.Assertion]) {
... // Same as in the question
val assertion: Future[compatible.Assertion]
override def apply(): Future[Assertion] = assertion
}
"Testing" should {
"be productive" in new IntAdder {
val assertion = Foo.doubleSum(adder, Seq(1, 2, 3)).map(sum => assert(sum == 12))
}()
}
}
However this also doesn't compile due to:
Error:(42, 10) ';' expected but '(' found.
}()
You could have a mix of the fixture-context objects and the loan-fixtures methods patterns.
Something like this:
class FooSpec extends AsyncWordSpec {
// Fixture-context object
trait IntAdder {
val adder = new Adder[Int] {
override implicit val num = IntIsIntegral
private var sum = Future.successful(num.zero)
override def add(number: Int): Unit = sum = sum.map(_ + number)
override def result: Future[Int] = sum
}
}
// Loan-fixture method
def withContext(testCode: IntAdder => Future[compatible.Assertion]): Future[compatible.Assertion] = {
val context = new IntAdder {}
testCode(context)
}
"Testing" should {
"be productive" in withContext { context =>
import context._
Foo.doubleSum(adder, Seq(1, 2, 3)).map(sum => assert(sum == 12))
}
}
}
I have the following code, written in Scala 2.10.0:
trait A[T <: B] {
self : { def foo() } =>
val action : ()=>Unit = this.foo _
//wanna make default for this
val construction : String=>T
def bar()(implicit x : String) : T = {
action()
val destination = construction(x)
destination.baz()
destination
}
}
trait B { def baz() {} }
class Xlass { def foo() {} }
class Klass(a : String)(implicit val x : String) extends B {
val f = new Xlass with A[Klass] {
//boilerplate!
val construction = new Klass(_)
}
}
implicit val x = "Something"
val destination = new Klass("some a").f.bar()
I wonder, is it possible to make a default for construction, such as val construction = new T(_)?
I've tried several options for now, but none of them works with all the characteristics of this code, such as use of type bounds, implicits and structural typing. As far as I could get is this, but it fails with scala.ScalaReflectionException: free type T is not a class:
import reflect.runtime.universe._
val tT = weakTypeTag[T]
...
val privConstruction =
x : String =>
runtimeMirror(tT.mirror.getClass.getClassLoader)
//fails here with scala.ScalaReflectionException: free type T is not a class
.reflectClass(tT.tpe.typeSymbol.asClass)
.reflectConstructor(tT.tpe.members.head.asMethod)(x).asInstanceOf[T]
So, finally, I did it:
trait A[T <: B] {
self : { def foo() } =>
val action : ()=>Unit = this.foo _
def construction(x: String)(implicit tag : reflect.ClassTag[T]) : T = {
tag.runtimeClass.getConstructor(classOf[String], classOf[String]).newInstance(x, x).asInstanceOf[T]
}
def bar()(implicit x : String, tag : reflect.ClassTag[T]) : T = {
action()
val destination = construction(x)
destination.baz()
destination
}
}
trait B { def baz() {} }
class Xlass { def foo() {} }
class Klass(a : String)(implicit val x : String) extends B {
val f = new Xlass with A[Klass]
}
implicit val x = "Something"
val destination = new Klass("some a").f.bar()