Override the whole class in scala - scala

I want to initialize a logistic regression with an outdated model, I want to use the tips given here Initializing logistic regression coefficients when using the Spark dataset-based ML APIs?, but the main method I want to use is private, how can I extend this class to override this private method or override the whole class?
I'm new to scala thanks for being lenient or at least kind
treated here Override whole class in java/scala but no satisfaying answer

Related

ScalaMock: Can't handle methods with more than 22 parameters (yet)

Scalamock reject my mocking attempt by saying that it doesnt support yet more than 22 methods.
The reason is because there are more than 22 methods, all together, in the class I am trying to mock (2 are mine, 20+ are mixed in (from Akka Json Support)).
Any ways to get round this limitation that doesnt involve rethinking the mixing part ?
I used it this way, with scalatest 3.0.2 :
override val apiClient: ApiClient = mock[ApiClient]
(apiClient.getById _).when(15538).returns("data")
Thank you !
I assume you don't actually want to test those JSON and other mixin functions, so I would suggest creating an abstract trait that defines your new, testable signatures and mixing it into your new class. This way you wouldn't need to change your design, and your clients of this ApiClient class could even decouple fully by using the trait type.
trait MyFunctionality {
def foo(): Unit
def somethingElse(i: Int): Int
}
class ApiClient extends Baseclass with Stuff with MoreStuff with MyFunctionality {
// ...
}
then
val m = mock[MyFunctionality]
(m.foo _).expects().once()
// etc
That way you additionally guard against any code being run in your class (or baseclass) constructors during unit test time.
Hope that helps.
I came out with the same solution at the end but I dont really like the noise it add to my very concise class. "C'est la vie" :)

How to programmatically generate an abstract Scala class extending an annotated trait?

Given an annotated trait, how should I go about generating an abstract class which implements the trait?
So, given the following user trait...
#Neuron
trait SomeTrait {
// ...
}
... in my library I want to insert something like the following next to it:
abstract class SomeTraitImpl extends SomeTrait
Note that I know nothing about the given trait except it's annotated with #Neuron.
I've tried to do this with ASM, implementing the concept explained in the answer to the question Using Scala traits with implemented methods in Java, but this concept scratches only the surface of what the Scala compiler emits as byte code. Even if I succeeded to master all possible combinations of var, val, lazy val, abstract override etc the odds are high that it will break with the next release of the Scala compiler.
So it looks like I should write a compile time macro instead. However, I am scratching my head over the documentation for Scala macros, so I wonder if anyone could draft something which gets me started? Any hint is appreciated, please!
The correct approach to do this is to make #Neuron a macro annotation (using the Macro Paradise compiler plugin) and implement the macro to do the code transformation. The resulting code is now part of Neuron DI, a framework for dependency injection which I wrote.

How to correctly implement a custom number-like class in Scala?

I am currently trying to implement my own UnsignedInt. I would like to implement this correctly so that it fits into the whole Scala type & class system. However, I am really confused by all the classes that fit into Number.
With which class(es) do I need to work: Numeric, Integral or ScalaNumber? Or something completely different? What classes and/or traits should my own class implement?
The short answer is: don't implement your own, use the Spire one :) Otherwise, you should implement Integral (which includes Numeric). Note that your type shouldn't extend it; you need implicit values in the companion object, i.e.
class UnsignedInt { ... }
object UnsignedInt {
implicit val UIntIntegral: Integral[UnsignedInt] = ...
}
You should also consider making your class a value class.

Scala Testing: Replace function implementation

Using ScalaTest, I want to replace a function implementation in a test case. My use case:
object Module {
private def currentYear() = DateTime.now().year.get
def doSomething(): Unit = {
val year = currentYear()
// do something with the year
}
}
I want to write a unit test testing Module.doSomething, but I don't want this test case to depend on the actual year the test is run in.
In dynamic languages I often used a construct that can replace a functions implementation to return a fixed value.
I'd like my test case to change the implementation of Module.currentYear to always return 2014, no matter what the actual year is.
I found several mocking libraries (Mockito, ScalaMock, ...), but they all only could create new mock objects. None of them seemed to be able to replace the implementation of a method.
Is there a way to do that? If not, how could I test code like that while not being dependent on the year the test is run in?
One way would be to just make do_something_with_the_year accessible to your test case (make it package protected for example). This is also nice because it separates looking up dependencies and actually using them.
Another way would be to put your logic in a trait, make the currentYear method protected and let the object be an instance of that trait. That way you could just create a custom object out of the trait it in a test and wouldn't need any mock library.
ScalaMock can mock singleton objects it says it right here: http://paulbutcher.com/2011/11/06/scalamock-step-by-step/
As well as traits (interfaces) and functions, it can also mock:
Classes
Singleton and companion objects (static methods) ...

Update immutable data structure through inheritance

I'm making a strategic game and I try to apply what I learned, try to use immutable data. In my game I have Units, these units can have different special function. By exemple some plane can hide themselves. What I search is a way to be able to do some sort of
abstract class Units {
val life:Int
}
trait Hidable { self: Units =>
val hided:Boolean
def hide:Units with Hidable= ....
}
without having to copy paste:
def hide = copy(hided=true)
on every case class that mixin Hidable.
A common way to update an immutable data structure -- is using lenses.
There's a compiler plugin to generate lenses for your code, though it is not very production-ready. It is also available for an old scalaz only.
Here's a related question.