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

Tagged unions / sealed classes #107

Open
thekid opened this issue Mar 13, 2021 · 1 comment
Open

Tagged unions / sealed classes #107

thekid opened this issue Mar 13, 2021 · 1 comment
Labels

Comments

@thekid
Copy link
Member

thekid commented Mar 13, 2021

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

@thekid
Copy link
Member Author

thekid commented Mar 13, 2021

First implementation idea:

// What the user writes
enum Distance {
  case Miles(public int $miles);
}

// What gets emitted
abstract class Distance implements \TaggedUnion {

  public static function Miles(int $miles): self {
    return new class('Miles', $miles) extends self {
      public $name, $miles;

      private function __construct($name, $miles) {
        $this->name= $name;
        $this->miles= $miles;
      }
    };
  }
}

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

No branches or pull requests

1 participant