Constructor default values are ignored

Issue #305 wontfix
Rubén Pozo created an issue

We have a class having a default value in the constructor, when Spring tries to resolve the object the default value is ignored:

A class like this:

  TDefaultValue = class(TInterfacedObject, IDefaultValue)
  private
    fFirst: Integer;
  public
    constructor Create(pFirst: Integer = 1);
    function GetFirst: Integer;
  end;

We are trying to get an instance with:

var
  defaultValue: IDefaultValue;
begin
  fContainer.RegisterType<TDefaultValue>
    .Implements<IDefaultValue>;
  fContainer.Build;
  defaultValue := fContainer.Resolve<IDefaultValue>;

defaultValue.GetFirst should return 1 but is returning 0

Comments (3)

  1. Stefan Glienke repo owner

    Unfortunately the RTTI that is used by the DI system does not provide any information about default parameter values thus it cannot pass them.

    So either make an overload with no parameter which then calls the one with parameter passing 1 or put the Inject attribute with value 1 (remember to put spring.container.common into the uses) onto the pFirst parameter. That will tell the container to pass 1 into it.

  2. Log in to comment