How can I override a parent method in coffeescript while still being able to call the parent - coffeescript

I have two coffeescript classes something like this. In the base view model I have a method that I want to override in the child that inherits from the base view model.
class exports.BaseViewModel
constructor: () ->
someBaseMethod: =>
console.log "I'm doing the base stuff"
class ChildViewModel extends BaseViewModel
constructor: () ->
someBaseMethod: =>
#doSomethingFirst()
super #someBaseMethod()
This isn't working as is because the line super #someBaseMethod() calls itself creating an infinite loop.
Is it possible to achieve what I want here?

Yes, call super just like it was a function (it represents a reference to the superclass version of the method you're in):
class ChildViewModel extends BaseViewModel
constructor: ->
someBaseMethod: =>
#doSomethingFirst()
super()

Related

Cannot create custom Appender for log4j2 in scala

I try to create a custom Appender in Scala (to use it in UTs), to migrate log4j1 to log4j2, but i cannot.
in log4j1 it's like:
class TestAppender extends AppenderSkeleton {
}
now i'm doing somting like:
class TestAppender extends AbstractAppender {
}
but i get
Cannot resolve overloaded constructor `AbstractAppender`
Any thoughts on how to do so?
In Java, you call the superclass constructor in your constructor with super(arguments). In Scala, however, because the body of the class (excluding defs) is the default constructor for the class, the superclass constructor is called by passing the arguments in the extends clause:
class TestAppender extends AbstractAppender(...) {
Since the only non-deprecated constructor for AbstractAppender appears to be this one, you almost certainly want something like:
class TestAppender extends AbstractAppender("test", someFilter, someLayout, whetherToIgnoreExceptions, Array()) {
If you want to allow the user of your class to specify a parameter for AbstractAppender, then you would do something like:
class TestAppender(ignoreExceptions: Boolean) extends AbstractAppender("test", someFilter, someLayout, ignoreExceptions, Array()) {

Writing a Python method to reference the Class attribute in the derived class, rather than base class

As someone who worked more in Java, I am having a bit of difficulty wrapping my head around polymorphic references to class attributes in Python.
What I would like to do is have a method in the base class which modifies a "static" variable (aka class attribute) of the base class, but when calling the method from the derived class, for the method to modify the class attribute of the derived class, not the base class. Note, I am NOT overriding the method in the derived class.
For example, I have something like:
class BaseClass:
itemList = []
def addItem(thing):
BaseClass.itemList.append(thing)
class Child1(BaseClass):
pass
class Child2(BaseClass):
pass
...
Child1.addItem("foo")
Child2.addItem("bar")
print(str(Child1.itemList))
I'd like: "foo"
I get: "foo, bar"
Now, I understand that because of "BaseClass.itemList.append(thing)", it will reference the class attribute of the base class.
Put another way, is there a way to avoid saying "BaseClass.itemList", but keep it static, or do I need to instead override the method in each of the child classes?
You can have a "static" class variable that can be changed by every instance of the class:
class BaseClass:
itemList = []
def addItem(self, thing):
self.itemList.append(thing)
class Child1(BaseClass):
itemList = []
class Child2(BaseClass):
itemList = []
# each class has its own "itemList"
# now we can instantiate each class and use the different itemLists:
c1 = Child1()
c1.addItem("foo")
c2 = Child2()
c2.addItem("bar")
c3 = Child1()
c3.addItem("foo2")
print(str(Child1.itemList)) # prints: ['foo', 'foo2']
print(str(Child2.itemList)) # prints: ['bar']

Calling a method of a super class through extended class leads into TypeError

I'm not sure if the title says it all.
What I'm trying to do is to have a super class (or a more or less abstract class) which then is used by another class which is extending itself from the super class.
Now what I did was to create a simple script:
class Animal
constructor: (#name, #sound) ->
setSound: (#sound) ->
sound: ->
alert "#{#sound}!!!"
name: ->
alert "#{#name}"
class Dog extends Animal
constructor: (#name) ->
#setSound "Woof"
doggy = new Dog "Max"
doggy.sound()
As you can see - this is just a crappy example but what happens is, when I now call doggy.sound() I get an TypeError in return.
You can see this script in action here: http://jsfiddle.net/zrVZb/1/
Am I doing something wrong or is this even a bug in coffeescript?
edit: I just saw, that it is contructor and not construct. But now I'm getting the TypeError instead.
Thanks
Your method is called sound. But then you pass an attribute with the same name, which overwrites this method. Try renaming your sound method into makeSound :
http://jsfiddle.net/V7Dx8/1/

Overriding toString in CoffeeScript class

I've made a class in CoffeeScript and I'd like to overwrite the toString() function; here's my code:
class MenuController
constructor: () ->
'constructor'
toString: () ->
'MenuController'
console.log MenuController.toString()
However toString() returns:
function MenuController() {
'constructor';
}
And not MenuController. What am I doing wrong?
You defined toString as a class method. So, you can call it as:
menuController = new MenuController()
menuController.toString()
If a static method is really what you want, then you should use the following syntax:
class MenuController
#toString: ->
'MenuController'
Then, you'll be able to call it like:
MenuController.toString()
But, it will no longer be a class method, so
(new MenuController()).toString()
won't call your toString method.

How to use a Scala sub-class constructor for a member within the super-class?

I want to invoke the constructor of the sub-class from the super-class (Tree[A]) to create additional instances of the sub-class of type A (Part or Project). It seems there must be a way that is better than the mess of creating the intermediate method newChild since things like List work. I have heard the maybe CanBuildFrom may address this problem.
Extract from working code:
abstract class Tree[A](parent: Option[A], key: Int) {
protected def newChild(aKey: Int): A
lazy val children: List[A] = childrenOf.map(newChild(_))
...
}
case class Part(parent: Option[Part], key: Int) extends Tree[Part](parent, key) {
protected def newChild(aKey: Int) = Part(Some(this), aKey)
...
}
case class Project(parent: Option[Project], key: Int) extends Tree[Project](parent, key) {
protected def newChild(aKey: Int) = Project(Some(this), aKey)
...
}
...
"*" #> <ul>{Part(None, pKey).expandAsHtml}</ul>
What is a better way to use a Scala sub-class constructor for a member within the super-class?
You're looking at it backwards. When an object is created, its constructor is called; this delegates to its superclass's constructor either by default or by explicit invocation. You can't call the subclass's constructor from the superclass's constructor, because it's the subclass's constructor that called the superclass's constructor in the first place.
You need to look at your logic and remember that the way a subclass is known is that it is being directly instantiated; it's not that the superclass is being instantiated with the subclass as some kind of parameter, instead the superclass is instantiated as a step in instantiating the subclass, and the superclass should only know about its own structure, leaving subclass details to the subclass constructor that triggered the superclass.
To my knowledge, the only way to achieve this is by using reflection.
I found a tutorial here: http://www.heimetli.ch/java/create-new-instance.html
This way you can get the constructor of the subclass within your superclass and call it to create a new instance of your subclass.
However you somehow would have to make sure that your subclasses implement constructors with a specific argument list.