Skip to content

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