Skip to content

Instantly share code, notes, and snippets.

@iluuu1994
Last active November 11, 2020 17:31
Show Gist options
  • Save iluuu1994/096af29df86b2fe9b3da95c14595a45e to your computer and use it in GitHub Desktop.
Save iluuu1994/096af29df86b2fe9b3da95c14595a45e to your computer and use it in GitHub Desktop.
// Literal pattern
$foo is 1;
$foo is 'foo';
$foo is SOME_CONST; // Ambiguous with class names :(
// Binding pattern
$foo is $bar;
// Type pattern
$foo is Foo;
$foo is int|string;
// Range pattern
$foo is 0..=10;
$foo is 0..<10;
$foo is >10;
$foo is >=10;
// Array pattern
$foo is [1, 2];
$foo is ['foo' => 0..=10];
$foo is ['foo', ...];
// Object pattern
$foo is Foo { bar: $bar };
$foo is Foo { $bar };
// Logical pattern
$foo is Equatable and Hashable;
$foo is 1 or 2;
// Wildcard pattern
$foo is _
// Regex pattern?
$foo is /^foo/;
// Expression pattern?
$foo is Foo { bar: (literallyAnything()) }
// Alternative to guards in other languages
match (foo) {
Foo { y } where y == literallyAnything() => ...,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment