diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 4176710c..10c3b4e0 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -85,7 +85,6 @@ - [Unchecked Casts](./unchecked-casts.md) - [Bugs and Unreachable Code](./bug-and-unreachable.md) - [Laziness](./laziness.md) - - [Type Match](./type-match.md) - [Purity Reflection](./purity-reflection.md) - [Type-Level Programming](./type-level-programming.md) - [Termination Checking](./termination-checking.md) diff --git a/src/advanced-features.md b/src/advanced-features.md index ed44e24b..0ff8c12d 100644 --- a/src/advanced-features.md +++ b/src/advanced-features.md @@ -4,6 +4,5 @@ In this chapter, we discuss some advanced features of Flix, including: - [Checked Casts](./checked-casts.md) and [Unchecked Casts](./unchecked-casts.md) - [The `bug!` and `unreachable!` functions](./bug-and-unreachable.md) -- [Type Match](./type-match.md) - [Purity Reflection](./purity-reflection.md) - [Termination Checking](./termination-checking.md) diff --git a/src/glossary.md b/src/glossary.md index 76ef5c5b..210d9eb5 100644 --- a/src/glossary.md +++ b/src/glossary.md @@ -56,9 +56,6 @@ Flix are type classes. ***Type Inference.*** A language feature that allows the compiler to infer the type of an expression without requiring annotations from the programmer. -***Type Match.*** A language feature that allows a function to inspect (reflect) -on a type. - ***Type Member.*** See [associated type](#associated-type). ***Unchecked Cast.*** An unsafe cast which is not verified by the compiler. Can diff --git a/src/type-match.md b/src/type-match.md deleted file mode 100644 index a4af2a63..00000000 --- a/src/type-match.md +++ /dev/null @@ -1,86 +0,0 @@ -# Type Match - -Flix supports a type match construct that enables compile-time pattern matching -on the type of a polymorphic value. - -For example, we can write a function that inspects the type of its argument: - -```flix -def inspect(x: a): String = typematch x { - case _: Int32 => "x is an Int32" - case _: String => "x is a String" - case _: _ => "x is neither an Int32 nor a String" -} - -def main(): Unit \ IO = - println(inspect(12345)); - println(inspect("abc")); - println(inspect(false)) -``` - -Here the `inspect` function pattern matches on the type of the formal parameter -`x` using the `typematch` construct. For example, if the type of `x` is an -`Int32` then the function returns the string `"x is an Int32"` and so forth. - -The `typematch` construct is eliminated at compile-time, hence there is no -runtime cost. - -As the example shows, the `typematch` construct always requires a default case. -This is because Flix has infinitely many types, and a `typematch` cannot cover -all of them. - -A type match can also inspect more complex types, as the following example -shows: - -```flix -def inspect(x: a): String = typematch x { - case _: List[Int32] => "x is a list of integers" - case _: List[String] => "x is a list of strings" - case _: _ => "x is something else" -} - -def main(): Unit \ IO = - println(inspect(1 :: 2 :: 3 :: Nil)); - println(inspect("abc" :: "def" :: Nil)); - println(inspect(false)) -``` - -We can also bind values with type match, as the following example shows: - -```flix -def inspect(x: a): String = typematch x { - case i: Int32 => "${i * i}" - case s: String => String.toUpperCase(s) - case _: _ => "-" -} - -def main(): Unit \ IO = - println(inspect(12345)); - println(inspect("abc")); - println(inspect(false)) -``` - -> **Warning:** While type match is a powerful meta-programming construct, it -> should be used sparingly and with great care. - -A typical legitimate use case for type match is when we want to work around -limitations imposed by the JVM. For example, the Flix Standard Library uses type -match to implement the `Array.copyOfRange` function as shown below: - -```flix -def copyOfRange(_: Region[r2], b: Int32, e: Int32, a: Array[a, r1]): ... = -typematch a { - case arr: Array[Int16, r1] => - import static java.util.Arrays.copyOfRange(Array[Int16, r1], Int32, Int32): ... - ... - case arr: Array[Int32, r1] => - import static java.util.Arrays.copyOfRange(Array[Int32, r1], Int32, Int32): ... - ... - // ... additional cases ... -} -``` - -Here type match allows us to call the right overloaded version of -`java.util.Arrays.copyOfRange`. Thus Flix programmers can use our version of -`copyOfRange` (i.e., `Array.copyOfRange`) while underneath the hood, we always -call the most efficient Java version.