When declaring a custom Aggregator (UDAF) in Spark, you can declare it either as a class or as an object:
object MyAgg extends Aggregator[IN, BUF, OUT] {...}
// VS
class MyAgg extends Aggregator[IN, BUF, OUT] {...}
In Spark docs, they declare it as an object. However, it can be convenient to declare it as a class if one wants to pass external parameters to it:
class MyAgg(parameter: Int) extends Aggregator[IN, BUF, OUT] {...}
Is there a method better than the other one or are they completely interchangeable?
Related
I am not able to call someMethod, it gives error
ReferenceError: Must call super constructor in derived class before
accessing 'this' or returning from derived constructor\
class BaseClass{
//some properties here I want to return whoever extends BaseClass
}
class ChildClass extend BaseClass{
constructor(){
super(
this.someMethod();
)
}
someMethod() {}
}
In your constructor, you have to call super() before you can use this:
class BaseClass{
//some properties here I want to return whoever calls ChildClass
}
class ChildClass extend BaseClass{
constructor(){
super()
this.someMethod();
}
someMethod() {}
}
There is no permitted structure for super(this.someMethod()) because you can't use this before you've called super(...). There is something like super.someMethod() which lets you call base class methods (without your local override), but that's not a substitute for calling super() in your constructor.
You can call super(someArgs) with arguments if you want and that would be whatever arguments you want to be passed to the base class constructor. But, you can't use this before doing so.
My viewModel contains some variables such as how many cardViews should be created in the recyclerView. Therefore, I am looking for a way to access the same viewModel object in my adapter class. Is there a way to do so or a better alternative? My code is in kotlin
class RecyclerAdapter : RecyclerView.Adapter<RecyclerAdapter.ViewHolder>() {
private val gameViewModel: GameViewModel by activityViewModels()
Since you have provided only 2 lines of code it is hard to know exactly what you are doing wrong.
Normally you would retrieve a ViewModel in an Activity class or a Fragment class like this
class MyActivity /* other stuff */ {
// this line produces/retrieves an instance of GameViewModel
// where its owner is MyActivity
private val gameViewModel: GameViewModel by viewModels()
}
Then somewhere else inside your activity class, you instantiate your RecycleAdapter class. There you would pass the gameViewModel to it. Of course, to be able to do that your RecyclerAdapter would either have to accept a GameViewModel as a constructor parameter, or through a setter, or some other function call.
Here is an example through a constructor parameter. Your RecyclerAdapter class would have to be defined something like this (note that this is Kotlin concise syntax for declaring properties and initializing them from the primary constructor)
class RecyclerAdapter(
private val gameViewModel: GameViewModel,
// add more constructor parameters/class properties here if needed
) : RecyclerView.Adapter<RecyclerAdapter.ViewHolder>() {
// other class properties that you don't want to initialize
// through the primary constructor
// ...
// the class body where you implement RecyclerView.Adapter<> methods
// ...
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
// gameViewModel can be used here
gameViewModel.doSomething()
}
}
And as a last step, modify the line in your code, where you create your RecyclerAdapter instance
// here we create a new RecyclerAdapter and pass the gameViewModel to it
val adapter = RecyclerAdapter(gameViewModel)
recyclerView.adapter = adapter
Your Fragment's views should have a shorter lifecycle than your associated ViewModel, so it should be OK to pass it in as a constructor parameter.
class RecyclerAdapter(private val gameViewModel: GameViewModel) :
RecyclerView.Adapter<RecyclerAdapter.ViewHolder>() {
//...
}
Then pass the view model reference in from the Fragment when you instantiate the adapter in onViewCreated().
Personally, I wouldn't do this because presumably your ViewModel has lots of stuff in it that is irrelevant to the Adapter. Separation of concerns. I would make parameters only for the properties that are needed and let the Fragment pass them along from the ViewModel.
When a static method defined on Base class is called using Child class, How to find it was called on Child class type?:
class Base {
static def method(){
println "class on which this method is called? ${this}"
}
}
class Child extends Base {}
Child.method()
In the code above, this rightly points to the Base class.
I don't think it can be done with actual static methods, but a nice alternative is to use the groovy expandoMetaClass to add a static closure method. Inside said closure, you can access the calling class as delegate. I.e.
Base.metaClass.static.anotherMethod = {
println "class on which another method is called? ${delegate}"
}
Calling now Base.anotherMethod() will have delegate referring to Base and calling Child.anotherMethod will have it pointing to Child.
I'm trying to use Groovy #groovy.transform.Immutable to implement classes with properties of unsupported "immutable" types. In my case it is java.io.File
For example, having class like
#groovy.transform.Immutable class TwoFiles {
File file1,file2
}
gives me following compile error
Groovyc: #Immutable processor doesn't know how to handle field 'file1' of type 'java.io.File' while compiling class TwoFiles.
#Immutable classes only support properties with effectively immutable types including:
- Strings, primitive types, wrapper types, BigInteger and BigDecimal, enums
- other #Immutable classes and known immutables (java.awt.Color, java.net.URI)
- Cloneable classes, collections, maps and arrays, and other classes with special handling (java.util.Date)
Other restrictions apply, please see the groovydoc for #Immutable for further details
One option I found it to extend java.io.File to make it Cloneable but I'm not happy with this solution. Following code compiles and works, but having own subclass of java.io.File is not what I'd like.
#groovy.transform.Immutable class TwoCloneableFiles {
FileCloneable file1,file2
class FileCloneable extends File implements Cloneable{
FileCloneable(String s) {
super(s)
}
// ... and other constructors ...
}
}
So my question is: Is there any other option how to use java.io.File directly in such class?
Possibly to mark java.io.File as "known immutable" for the purpose of #groovy.transform.Immutable (same as it seems to be done for java.awt.Color, java.net.URI)?
Have you tried using knownImmutableClasses to specify File? Something like this should work:
#groovy.transform.Immutable(knownImmutableClasses = [File])
class TwoFiles {
File file1,file2
}
(With File, you could probably also get rougly the effect you want with the following:
#groovy.transform.Immutable
class TwoFiles {
String file1,file2
public File getFile1() {return new File(file1)}
public File getFile2() {return new File(file2)}
}
def f = new TwoFiles("/", "/Users")
assert f.file1.class == File
)
I would like to understand why when I call protected method, declared and implemented in base class, from derived class via pointer to base class I get compilation error (C2248) and when I call it from derived class via pointer to derived class instance, compilation pass.
I understand it is part of the language but I want to understand why
My explanation is that when I call a protected member of base class via pointer to base class in derived class, compilation fails because the inheritance of base class can be protected or private but when I call it via pointer to derived class in a derived class it o.k because it is part of the class.
Is that right?
e.g.
class Base
{
protected:
virtual void func() {}
}
class Derived : public Base
{
public:
virtual void myMethod()
{
Base* pBase = new Base;
pBase->func(); -> compilation error (C2248)
Derived* pDerived = new Derived;
pDerived->func(); -> O.K
}
}
The failing line is not compilable because you are accessing an instance of Base - only public methods can be accessed this way. If you do this in myMethod():
Base::func();
it should compile because now we are accessing the inherited method for this. Kind of weird to have pDerived::myMethod() call the Derived constructor?