Skip to content

Tagged unions / sealed classes #107

Description

@thekid

See https://wiki.php.net/rfc/tagged_unions:

// Declaration
enum Distance {
  case Kilometers(public int $km);
  case Miles(public int $miles);
}

// Usage
$walk= Distance::Miles(500);
print $walk->miles; // prints "500"

The famous "Maybe" nomad:

// Declaration
enum Optional {
  case None {
    public function bind(callable $f) { return $this; }
    public function value(): mixed { return null; }
  };
 
  case Some(private mixed $value) {
    public function bind(callable $f): Optional { return Optional::of($f($this->value)); }
    public function value(): mixed { return $this->value; }
  };

  public static function of($value): self {
    return $value instanceof self ? $value : ($value ? self::Some($value) : self::None);
  }
}

// Usage
$value= Optional::of($record)->bind(fn($r) => $r['distance'] + 1)->value();

These are very similar to Kotlin's sealed classes: In some sense, sealed classes are similar to enum classes: the set of values for an enum type is also restricted, but each enum constant exists only as a single instance, whereas a subclass of a sealed class can have multiple instances, each with its own state..

See also https://www.dotnetcurry.com/patterns-practices/1510/maybe-monad-csharp

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions