Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntactic support for type aliases #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

thekid
Copy link
Member

@thekid thekid commented Oct 26, 2023

In a nutshell:

use numeric = string|int;

function multiply(numeric $a, numeric $b) {
  return bcmul($a, $b);
}

See also:

@thekid
Copy link
Member Author

thekid commented Oct 26, 2023

A reusable type system could be created by creating a type type.

// Basic form
type Numeric extends string|float|int;

// Type casts
type Numeric extends string|float|int {
  public static function cast(parent $in): self {
    is_numeric($in) || throw new ClassCastException($in.' is not numeric');
    return $in;
  }
}

$times= (Numeric)($argv[1] ?? 10);

// Extension methods
type User extends array<string, mixed> {
  public function id(): UUID { return new UUID($this['_id']); }
  public function name(): string { return $this['name']; }
}

$admin= (User)['_id' => '691522c4-2f24-4264-ba75-4df62b931422', 'name' => 'Admin'];
$admin->id();  // util.UUID{691522c4-2f24-4264-ba75-4df62b931422}

See also:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant