Skip to main content
added 75 characters in body
Source Link
CodesInChaos
  • 5.9k
  • 4
  • 24
  • 28

For the caller a constructor behaves similar to a static method that returns an instance. But from the inside a constructor behaves like a void returning instance method, so not being able to specify a value on the return statement is only consistent.

You could mentally model it as new XYZ() first creating an instance initialized to zero in compiler-generated-code and then calling the user-written constructor on that instance, just like a normal instance method.

Something like:

public static T create() // Compiler generated
{
    T instance = AllocateZeroed<T>(); 

    instance.field1 = ABC; // field initializers
    instance.field2 = DEF; // field initializers        
    ...

    instance.constructor(); // user-written constructor chain 

    return instance;
}

For the caller a constructor behaves similar to a static method that returns an instance. But from the inside a constructor behaves like a void returning instance method, so not being able to specify a value on the return statement is only consistent.

You could mentally model it as new XYZ() first creating an instance initialized to zero in compiler-generated-code and then calling the user-written constructor on that instance, just like a normal instance method.

Something like:

public static T create() // Compiler generated
{
    T instance = AllocateZeroed<T>();
    instance.constructor(); // user-written constructor chain
    return instance;
}

For the caller a constructor behaves similar to a static method that returns an instance. But from the inside a constructor behaves like a void returning instance method, so not being able to specify a value on the return statement is only consistent.

You could mentally model it as new XYZ() first creating an instance initialized to zero in compiler-generated-code and then calling the user-written constructor on that instance, just like a normal instance method.

Something like:

public static T create() // Compiler generated
{
    T instance = AllocateZeroed<T>(); 

    instance.field1 = ABC; // field initializers
    instance.field2 = DEF; // field initializers        
    ...

    instance.constructor(); // user-written constructor chain 

    return instance;
}
Source Link
CodesInChaos
  • 5.9k
  • 4
  • 24
  • 28

For the caller a constructor behaves similar to a static method that returns an instance. But from the inside a constructor behaves like a void returning instance method, so not being able to specify a value on the return statement is only consistent.

You could mentally model it as new XYZ() first creating an instance initialized to zero in compiler-generated-code and then calling the user-written constructor on that instance, just like a normal instance method.

Something like:

public static T create() // Compiler generated
{
    T instance = AllocateZeroed<T>();
    instance.constructor(); // user-written constructor chain
    return instance;
}