-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: give type casts the lowest precedence (#1157)
Closes #1150 ### Summary of Changes Type casts now have the lowest precedence. In order to use them inside other expressions, they **must be enclosed in parentheses**.
- Loading branch information
1 parent
11c81b3
commit 7549fa1
Showing
8 changed files
with
61 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,31 @@ | ||
## Type Casts | ||
# Type Casts | ||
|
||
The compiler can _infer_ the [type][types] of an expression in almost all cases. However, sometimes its [type][types] | ||
has to be specified explicitly. This is called a _type cast_. Here is an example: | ||
The compiler can _infer_ the [type][types] of an expression in almost all cases. However, sometimes its type must be | ||
specified explicitly. This is called a _type cast_. Here is an example: | ||
|
||
```sds | ||
dataset.getColumn("age") as Column<Int> | ||
table.getColumn("age") as Column<Int> | ||
``` | ||
|
||
A type cast is written as follows: | ||
|
||
- The expression to cast. | ||
- The keyword `#!sds as`. | ||
- The [type][types] to cast to. | ||
- The type to cast to. | ||
|
||
Type casts are only allowed if the type of the expression is unknown. They cannot be used to override the inferred type | ||
of an expression. | ||
Afterward, the compiler will treat the expression as if it had the specified type. If the expression's actual type is | ||
not compatible with the specified type, the compiler will raise an error. | ||
|
||
!!! warning "Precedence" | ||
Type casts have the lowest precedence of all operators. If you want to use a type cast in an expression, you must | ||
enclose it in parentheses: | ||
|
||
```sds | ||
(row.getValue("age") as Int) < 18 | ||
``` | ||
|
||
This is necessary, because the less than operator (`<`) looks the same as the opening angle bracket of a type | ||
argument list (`Column<Int>`). We could remove this ambiguity by using different syntax for the less than operator | ||
or for type argument lists, but both are established conventions in other languages. | ||
|
||
[types]: ../types.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
...sts/resources/grammar/expressions/type casts/bad-in expression without parentheses.sdsdev
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// $TEST$ syntax_error | ||
|
||
pipeline myPipeline { | ||
1 as Int + 1; | ||
} |
2 changes: 1 addition & 1 deletion
2
packages/safe-ds-lang/tests/resources/grammar/expressions/type casts/good-chained.sdsdev
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
// $TEST$ no_syntax_error | ||
|
||
pipeline myPipeline { | ||
1 as Int as String; | ||
(1 as Int) as String; | ||
} |
5 changes: 5 additions & 0 deletions
5
...s-lang/tests/resources/grammar/expressions/type casts/good-in less than comparison.sdsdev
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// $TEST$ no_syntax_error | ||
|
||
pipeline myPipeline { | ||
(1 as Int) < 2; | ||
} |