call one constructor from another constructors in one class - constructor

I've encountered the following question online.
If we call one constructor from another in a class, what will happen?
Can anyone give me some hints?

in java also its possible with the power of the this keyword. check out the example given below.
public class A {
public A() {
//this("a");
System.out.println("inside default Constructor");
}
public A(String a){
this();
System.out.println("inside Constructor A");
}
}
This concept is called constructor chaining. If it's c# i found this saying it's possible Is nesting constructors (or factory methods) good, or should each do all init work

This example from MSDN clarifies it
To add delegating constructors, constructor (. . .) : constructor (. . .) syntax is used.
class class_a {
public:
class_a() {}
// member initialization here, no delegate
class_a(string str) : m_string{ str } {}
// can’t do member initialization here
// error C3511: a call to a delegating constructor shall be the only member-initializer
class_a(string str, double dbl) : class_a(str) , m_double{ dbl } {}
// only member assignment
class_a(string str, double dbl) : class_a(str) { m_double = dbl; }
double m_double{ 1.0 };
string m_string;
};
Read answers from Can I call a constructor from another constructor (do constructor chaining) in C++? too.

Related

Pass method as argument to Java method

I have some java code that I want to convert to Xtend. The Java code is:
public void createPartControl(Composite parent) {
final Canvas clock = new Canvas(parent, SWT.None);
clock.addPaintListener(this::drawClock);
}
private void drawClock(PaintEvent e) {
e.gc.drawArc(e.x, e.y, e.width - 1, e.height - 1, 0, 360);
}
My attempt at Xtend code is:
override createPartControl(Composite parent) {
val clock = new Canvas(parent, SWT.None);
clock.addPaintListener(this::drawClock);
}
private def drawClock(PaintEvent e) {
e.gc.drawArc(e.x, e.y, e.width - 1, e.height - 1, 0, 360);
}
The problem is that the expression this::drawClock is not valid in Xtend. Specifically it says that this cannot be resolved to a type. How do I achieve the same result using Xtend.
Xtend doesn't support method references. You need to wrap the method you want to call in a Xtend lambda instead.
this::drawClock or (e) -> drawClock(e) in Java becomes [drawClock] in Xtend. The type is automatically inferred.
So you can write:
override createPartControl(Composite parent) {
val clock = new Canvas(parent, SWT.None)
clock.addPaintListener[drawClock]
}
The PaintEvent parameter of the listener is the implicit it parameter of the lambda. It is used automatically as first argument of drawClock. Parenthesis are optional for method calls with lambda arguments.
I would say the notation addPaintListener[drawClock] is the idiomatic, but you could of course also write this more explicit. All of these are equivalent:
clock.addPaintListener[drawClock]
clock.addPaintListener([drawClock])
clock.addPaintListener([drawClock(it)])
clock.addPaintListener([e|drawClock(e)])
clock.addPaintListener([PaintEvent e| e.drawClock])
If the method has multiple parameters, you can add those parameters to the lambda explicitly or use the implicit $1,$2,...,$n parameters, for example like this:
component.addFooListener[e1, e2| onFoo(e1, e2)]
component.addFooListener[onFoo($1, $2)]

Using 'this' in Kotlin initializer - alternative solution, or safe to ignore?

I have a class that registers itself as an event handler, with an event service:
interface CommunicationService {
fun sendActivationMessage(toEmail: String)
}
abstract class EventCommunicationService : CommunicationService, AbstractEventHandler {
constructor(eventService: EventService) {
eventService.registerListener(this)
}
override fun onEvent(event: Event) {
if (event.type == EventType.USER_CREATED) {
sendActivationMessage(event.userEmail)
}
}
}
The idea being there can be an EmailCommunicationService, or a mocked testing version, etc. which don't all need to register themselves as listeners for when a user is created.
However Kotlin complains that I'm:
Leaking 'this' in constructor of non-final class EventCommunicationService
Which, well, I am. I could easily ignore the warning - but is there a better approach?
I've tried using an init { } block instead of a constructor, but the warning is the same.
I basically want a "post-construct" callback or similar that can be used to let this service register itself with the EventService provided in the constructor since that's the point of this intermediate type.
I understand why this is a problem - but I'm not sure how to reason my way to fixing it.
init blocks are really part of the constructor (in JVM terms), so that wouldn't help with the problem. It is very much not safe to ignore in general: see Leaking this in constructor warning for reasons (just ignore the accepted answer, its comments contain the real meat and so does Ishtar's answer).
One option (assumes that all subclasses have no-argument constructors, though it could be extended):
abstract class <T : EventCommunicationService> EventCommunicationServiceCompanion(private val creator: () -> T) {
operator fun invoke(eventService: EventService): T {
val obj = creator()
eventService.registerListener(obj)
return obj
}
}
// a subclass of EventCommunicationService:
class MyService private constructor () : EventCommunicationService {
companion object : EventCommunicationServiceCompanion<MyService>(MyService::new)
}
To create a MyService, you still call MyService(eventService), but this is actually the companion object's invoke method and not the constructor.

When redirecting to another constructor in Dart, is it required to provide all the arguments?

So, in Ruby, if I call super without arguments, it automatically passes all the arguments from the current method to super. Is this possible to do in Dart?
Example how it's done:
class SomeClass {
SomeClass(arg1, arg2) {}
SomeClass.build(arg1, arg2) : super(arg1, arg2);
}
Could it be done like this:
class SomeClass {
SomeClass(arg1, arg2) {}
SomeClass.build : super;
}
or in some similar way?
No, you have to provide all the arguments.

Castle Windsor Resolve method: why pass arguments? What are they for?

I am confused by Castle Windsor resolve method. This method allows me to pass almost anything. Is the value submitted in the resolve method passed along and used in the constructor of the object which is eventually resolved to, or is this value used to help the resolver determine what concrete implementation to use?
For example, if I have the following snippet...
var _container = new WindsorContainer();
_container.Install(FromAssembly.This());
var MyProcessor = _container.Resolve<IProcessor>(new Arguments(new {"Processor1"}));
assuming I have two concrete implementations of IProcessor - like Processor1:IProcessor, and/or Processor2:IProcessor. What are the 'Arguments' used for?
I understand the...
Component.For<IProcessor>()
... needs to be defined, but I am struggling with the terms the Windsor folks choose to use (i.e. DependsOn, or ServicesOverrides) and the intent. Given the method is called 'resolve' I can only image any values passed to this will be used to resolve the decision on which concrete implementation to use. Is this assumption wrong?
The arguments parameter you're talking about is for providing arguments to components that can't be satisfied by Windsor components. The anonymous types overloads as well as the dictionary overalls I believe are all for this purpose. I've used this in the past, and I don't recommend it as it leads to poor patterns like Cristiano mentioned... and last time I used this it only works for the component being directly resolved. Anyway... here's an example of how this works:
[TestFixture]
public class Fixture
{
[Test]
public void Test()
{
IWindsorContainer container = new WindsorContainer();
container.Register(Component.For<IFoo>().ImplementedBy<Foo>().LifeStyle.Is(LifestyleType.Transient));
Assert.Throws<HandlerException>(() => container.Resolve<IFoo>());
IFoo foo = container.Resolve<IFoo>(new {arg1 = "hello", arg2 = "world"});
Assert.That(foo, Is.InstanceOf<Foo>());
Assert.That(foo.ToString(), Is.EqualTo("hello world"));
}
}
public interface IFoo
{
}
public class Foo : IFoo
{
private readonly string _arg1;
private readonly string _arg2;
public Foo(string arg1, string arg2)
{
_arg1 = arg1;
_arg2 = arg2;
}
public override string ToString()
{
return string.Format("{0} {1}", _arg1, _arg2);
}
}
I am awarding the answer to kellyb for the awesome example. During investigation, using Castle.Windsor 3.2.1, I found at least 2 reasons for passing a value in the "resolve" method.
To satisfy intrinsic type dependencies, such as strings, or integers in
the object resolved by the use of the "Resolve" method - as
described in kellyb's example.
To help Castle identify which concrete implementation to select.
to help illustrate both uses I am elaborating on the example provided above by kellyb.
Synopsis - or test condition
Assume there is one interface called IFoo and two concrete implementations that derive from this interface called Foo and Bar. A class called Baz is defined but does not derive from anything. Assume Foo requires two strings, but Bar requires a Baz.
Interface IFoo Definition
namespace CastleTest
{
public interface IFoo
{
}
}
Class Foo Definition
namespace CastleTest
{
public class Foo : IFoo
{
private readonly string _arg1;
private readonly string _arg2;
public Foo(string arg1, string arg2)
{
_arg1 = arg1;
_arg2 = arg2;
}
public override string ToString()
{
return string.Format("{0} {1}", _arg1, _arg2);
}
}
}
Class Bar Definition
namespace CastleTest
{
class Bar : IFoo
{
private Baz baz;
public Bar(Baz baz)
{
this.baz = baz;
}
public override string ToString()
{
return string.Format("I am Bar. Baz = {0}", baz);
}
}
}
Class Baz Definition
namespace CastleTest
{
public class Baz
{
public override string ToString()
{
return "I am baz.";
}
}
}
The Test (Drum roll, please!)
kellyb's example test shows an assert that expects a failure if args is not supplied. kellyb's example does not have multiple implementations registered. My example has multiple implementations registered, and depending on which is marked as the default this assert may or may not fail. For example, if the concrete implementation named "AFooNamedFoo" is marked as default, the assert completes successfully - that is to say the resolution of an IFoo as a Foo does indeed require args to be defined. If the concrete implementation named "AFooNamedBar" is marked as default, the assertion fails - that is to say the resolution of an IFoo as a Bar does not require args to be defined because its dependency of a Baz is already registered (in my example where multiple concrete implementations are registered). For this reason, I have commented out the assert in my example.
using Castle.Core;
using Castle.MicroKernel.Handlers;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using NUnit.Framework;
namespace CastleTest
{
[TestFixture]
public class ArgsIdentifyConcreteImplementation
{
[Test]
public void WhenSendingArgsInResolveMethodTheyAreUsedToIdentifyConcreteImplementation()
{
IWindsorContainer container = new WindsorContainer();
container.Register(Component.For<IFoo>().ImplementedBy<Foo>().LifeStyle.Is(LifestyleType.Transient).Named("AFooNamedFoo"));
container.Register(Component.For<IFoo>().ImplementedBy<Bar>().LifeStyle.Is(LifestyleType.Transient).Named("AFooNamedBar").IsDefault());
container.Register(Component.For<Baz>().ImplementedBy<Baz>().LifeStyle.Is(LifestyleType.Transient));
// THIS ASSERT FAILS IF AFooNamedBar IS DEFAULT, BUT
// WORKS IF AFooNamedFoo IS DEFAULT
//Assert.Throws<HandlerException>(() => container.Resolve<IFoo>());
// RESOLVE A FOO
IFoo foo = container.Resolve<IFoo>("AFooNamedFoo", new { arg1 = "hello", arg2 = "world" });
Assert.That(foo, Is.InstanceOf<Foo>());
Assert.That(foo.ToString(), Is.EqualTo("hello world"));
// RESOLVE A BAR
IFoo bar = container.Resolve<IFoo>("AFooNamedBar");
Assert.That(bar, Is.InstanceOf<Bar>());
Assert.That(bar.ToString(), Is.EqualTo("I am Bar. Baz = I am baz."));
}
}
}
Conclusion
Looking at the test above, the resolution of a Foo object has two things passed in the "resolve" method - the name of the implementation, and the additional string dependencies as an IDictionary object. The resolution of a Bar object has one thing passed in the "resolve" method - the name of the implementation.
In fact you should not call Resolve ever in your code rather than in the Composition root and also there you should not need to supply parameters to the Resolve method.
Custom resolution strategies should be done through installers, factories/ITypedFactoryComponentSelector, subresolvers... see documentation for more details on those options
BTW through "Resolve" parameters you can identify the component to resolve(by name or type) and its own direct dependencies.

calling super() from an actionscript constructor with varargs

If a constructor takes its parameters as a vararg (...) it seems to be impossible to create a subclass that will just pass on that vararg to the superclass.
There is a related question with fix for this same situation for normal functions: Wrapping a Vararg Method in ActionScipt but I cannot get that to work with a super call.
base class:
public class Bla
{
public function Bla(...rest)
{
trace(rest[0]); // trace the first parameter
}
}
subclass:
public class Blie extends Bla
{
public function Blie(...rest)
{
// this is not working, it will
// pass an array containing all
// parameters as the first parameters
super(rest);
}
}
if I now call
var b1 = new Bla('d', 'e');
var b2 = new Blie('a', 'b', 'c');
I get the output
d
a,b,c
And I want it to print out:
d
a
Aside from actually moving the handling of the parameters to the subclass or shifting it off to a separate initializer method, does anyone know how to get the super call right?
There's unfortunately no way to call the super constructor with ... args. If you remove the super() call, it will be called by the compiler (with no arguments). arguments is also not accessible from constructors.
If you can change the method signatures, you modify the arguments to accept an Array rather than ... args. Otherwise, as you mentioned, you could move it into an initializer method.
You may use a statement like this:
override public function doSomething(arg1:Object, ...args):void {
switch(args.length) {
case 0: super.doSomething(arg1); return;
case 1: super.doSomething(arg1, args[0]); return;
case 2: super.doSomething(arg1, args[0], args[1]); return;
}
}