Skip to content

Compact assignment syntax #240

@thekid

Description

@thekid

Scope of Change

A compact assignment syntax for member initialization in constructors and methods will be added.

Rationale

Less $this.member= $member; typing - especially in value objects.

Functionality

The $this.member notation is used to describe the argument passed to this parameter will end up in the member variable named member.

public class Person {
  protected string $name;

  public __construct($this.name) { }
}

// Equivalent of
public class Person {
  protected string $name;

  public __construct(string $name) { 
    $this.name= $name;
  }
}

This notation may be combined with regular parameters:

public class Person {
  protected static int $nextId= 0;

  protected int $id;
  protected string $name;

  public __construct($this.name, int $id= -1) { 
    $this.id= -1 === $id ? ++self::$nextId : $id;
  }
}

Security considerations

n/a

Speed impact

None, resolved during compile time.

Dependencies

n/a

Related documents

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions