Menu

#91 'this' has limitations inside bl constructor

open
nobody
invalid (1)
2022-11-01
2022-11-01
Anonymous
No

Originally created by: ellik95

Subject of the issue

this has limitations of usage inside the constructor. See an example below:

Steps to reproduce

If anyone types this in bl:

ValueObject TitleVO {
  constructor(props: TitleProps): (OK(TitleVO), Errors(DomainErrors.TitleOutOfBoundsError)) {
    this.title = this.toUpperCase(props.title);
    applyRules(TitleOutOfBoundsRule(this.title));
  }

  private toUpperCase() {...}
}

In typescript will be generated this:

export class TitleVO extends Domain.ValueObject<TitleProps> {
  get title(): string {
    return this.props.title;
  }

  private constructor(props: TitleProps) {
    super(props);
    this.props.title = this.toUpperCase(props.title);
  }

  public static create(props: TitleProps): Either<TitleVO, DomainErrors.TitleOutOfBounds> {
    const res = Domain.applyRules([new Rules.TitleOutOfBounds(this.props.title)]);
    if (res) return fail(res);
    return ok(new TitleVO(props));
  }

  private toUpperCase(title: string) {...}
}

Expected behaviour

I should be able to do something like the above in bl.

Actual behaviour

this.props.title in create will produce an error, because it is set in the static create method and this is not set yet.

Suggested solutions?

Redesign bl language to have both constructor and create in domain?

Discussion


Log in to post a comment.