From 3dc9eaef5db090750691c9732239f82b9778ebd0 Mon Sep 17 00:00:00 2001 From: AndersJensenStudie Date: Wed, 11 Feb 2026 21:25:20 +0100 Subject: [PATCH] Fixed checked- and unchecked-casting to use new syntax --- src/checked-casts.md | 26 +++++++++++++++----------- src/unchecked-casts.md | 6 +++--- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/checked-casts.md b/src/checked-casts.md index c0c381e6..7416f299 100644 --- a/src/checked-casts.md +++ b/src/checked-casts.md @@ -16,9 +16,11 @@ Flix has two _safe_ upcast constructs: The following program: ```flix +import java.lang.Object + def main(): Unit = let s = "Hello World"; - let o: ##java.lang.Object = s; + let _: Object = s; () ``` @@ -27,11 +29,11 @@ does not compile: ``` ❌ -- Type Error -------------------------------------------------- ->> Expected type: 'Object' but found type: 'String'. +>> Unexpected type: expected 'java.lang.Object', found 'String'. -4 | let o: ##java.lang.Object = s; - ^ - expression has unexpected type. +5 | let _: Object = s; + ^ + expression has unexpected type. ``` because in Flix the `String` type is _not_ a subtype of `Object`. @@ -39,9 +41,11 @@ because in Flix the `String` type is _not_ a subtype of `Object`. We can use a checked type cast to safely upcast from `String` to `Object`: ```flix +import java.lang.Object; + def main(): Unit = let s = "Hello World"; - let o: ##java.lang.Object = checked_cast(s); + let _: Object = checked_cast(s); () ``` @@ -49,11 +53,11 @@ We can use the `checked_cast` construct to safely upcast any Java type to one of its super-types: ```flix -let _: ##java.lang.Object = checked_cast("Hello World"); -let _: ##java.lang.CharSequence = checked_cast("Hello World"); -let _: ##java.io.Serializable = checked_cast("Hello World"); -let _: ##java.lang.Object = checked_cast(null); -let _: ##java.lang.String = checked_cast(null); +let _: Object = checked_cast("Hello World"); +let _: CharSequence = checked_cast("Hello World"); +let _: Serializable = checked_cast("Hello World"); +let _: Object = checked_cast(null); +let _: String = checked_cast(null); ``` ## Checked Effect Casts diff --git a/src/unchecked-casts.md b/src/unchecked-casts.md index c6a9edb0..529df98c 100644 --- a/src/unchecked-casts.md +++ b/src/unchecked-casts.md @@ -16,7 +16,7 @@ Flix programmers should normally never need to use an unchecked type cast. The expression below casts a `String` to an `Object`: ```flix -unchecked_cast("Hello World" as ##java.lang.Object) +unchecked_cast("Hello World" as Object) ``` Note: It is safer to use the `checked_cast` expression. @@ -26,7 +26,7 @@ Note: It is safer to use the `checked_cast` expression. The expression below casts the `null` value (of type `Null`) to `String`: ```flix -unchecked_cast(null as ##java.lang.String) +unchecked_cast(null as String) ``` Note: It is safer to use the `checked_cast` expression. @@ -37,7 +37,7 @@ The expression below contains an illegal cast and triggers a `ClassCastException` at runtime: ```flix -unchecked_cast((123, 456) as ##java.lang.Integer) +unchecked_cast((123, 456) as Integer) ``` ## Effect Casts