From f9f76bcb98226058b495a8ca982b4fa24a530a57 Mon Sep 17 00:00:00 2001 From: Lars Reimann Date: Thu, 2 May 2024 18:47:07 +0200 Subject: [PATCH] feat: convert any value to a boolean --- docs/api/safeds/lang/Any.md | 50 ++++++++++++++++++- docs/api/safeds/lang/Boolean.md | 2 +- docs/api/safeds/lang/Float.md | 4 +- docs/api/safeds/lang/Int.md | 4 +- docs/api/safeds/lang/List.md | 8 +-- docs/api/safeds/lang/Map.md | 8 +-- docs/api/safeds/lang/Nothing.md | 2 +- docs/api/safeds/lang/Number.md | 2 +- docs/api/safeds/lang/String.md | 38 +++++++------- .../builtins/safeds/lang/coreClasses.sdsstub | 17 +++++++ .../generation/python/macros/any/input.sdsdev | 6 +++ 11 files changed, 106 insertions(+), 35 deletions(-) create mode 100644 packages/safe-ds-lang/tests/resources/generation/python/macros/any/input.sdsdev diff --git a/docs/api/safeds/lang/Any.md b/docs/api/safeds/lang/Any.md index 06926f70a..0f7c65aed 100644 --- a/docs/api/safeds/lang/Any.md +++ b/docs/api/safeds/lang/Any.md @@ -12,6 +12,23 @@ The common superclass of all classes. ```sds linenums="6" class Any { + /** + * Return whether the object is truthy. + * + * @example + * pipeline example { + * val boolean = 1.toBoolean(); // true + * } + * + * @example + * pipeline example { + * val boolean = 0.toBoolean(); // false + * } + */ + @Pure + @PythonMacro("bool($this)") + fun toBoolean() -> boolean: Boolean + /** * Return a string representation of the object. * @@ -26,6 +43,37 @@ The common superclass of all classes. } ``` +## `#!sds fun` toBoolean {#safeds.lang.Any.toBoolean data-toc-label='toBoolean'} + +Return whether the object is truthy. + +**Results:** + +| Name | Type | Description | +|------|------|-------------| +| `boolean` | [`Boolean`][safeds.lang.Boolean] | - | + +**Examples:** + +```sds hl_lines="2" +pipeline example { + val boolean = 1.toBoolean(); // true +} +``` +```sds hl_lines="2" +pipeline example { + val boolean = 0.toBoolean(); // false +} +``` + +??? quote "Stub code in `coreClasses.sdsstub`" + + ```sds linenums="21" + @Pure + @PythonMacro("bool($this)") + fun toBoolean() -> boolean: Boolean + ``` + ## `#!sds fun` toString {#safeds.lang.Any.toString data-toc-label='toString'} Return a string representation of the object. @@ -46,7 +94,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="16" + ```sds linenums="33" @Pure @PythonMacro("str($this)") fun toString() -> string: String diff --git a/docs/api/safeds/lang/Boolean.md b/docs/api/safeds/lang/Boolean.md index e27e08c39..2806a6232 100644 --- a/docs/api/safeds/lang/Boolean.md +++ b/docs/api/safeds/lang/Boolean.md @@ -22,6 +22,6 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="39" + ```sds linenums="56" class Boolean ``` diff --git a/docs/api/safeds/lang/Float.md b/docs/api/safeds/lang/Float.md index 4e9141528..2ce60db29 100644 --- a/docs/api/safeds/lang/Float.md +++ b/docs/api/safeds/lang/Float.md @@ -19,7 +19,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="77" + ```sds linenums="94" class Float sub Number { /** @@ -56,7 +56,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="87" + ```sds linenums="104" @Pure @PythonMacro("int($this)") fun toInt() -> int: Int diff --git a/docs/api/safeds/lang/Int.md b/docs/api/safeds/lang/Int.md index 89952b04a..48df8de54 100644 --- a/docs/api/safeds/lang/Int.md +++ b/docs/api/safeds/lang/Int.md @@ -19,7 +19,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="54" + ```sds linenums="71" class Int sub Number { /** @@ -56,7 +56,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="64" + ```sds linenums="81" @Pure @PythonMacro("float($this)") fun toFloat() -> float: Float diff --git a/docs/api/safeds/lang/List.md b/docs/api/safeds/lang/List.md index 685f46b07..517896f04 100644 --- a/docs/api/safeds/lang/List.md +++ b/docs/api/safeds/lang/List.md @@ -23,7 +23,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="100" + ```sds linenums="117" class List { /** @@ -103,7 +103,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="115" + ```sds linenums="132" @Pure @PythonMacro("$separator.join($this)") fun join(separator: String = ", ") -> string: String @@ -129,7 +129,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="142" + ```sds linenums="159" @Pure @PythonMacro("len($this)") fun size() -> size: Int @@ -162,7 +162,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="130" + ```sds linenums="147" @Pure @PythonMacro("$this[$start:$end]") fun slice(start: Int = 0, end: Int = this.size()) -> slice: List diff --git a/docs/api/safeds/lang/Map.md b/docs/api/safeds/lang/Map.md index 1b99d7af1..f2a17326b 100644 --- a/docs/api/safeds/lang/Map.md +++ b/docs/api/safeds/lang/Map.md @@ -28,7 +28,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="159" + ```sds linenums="176" class Map { /** @@ -109,7 +109,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="191" + ```sds linenums="208" @Pure @PythonMacro("list($this.keys())") fun keys() -> keys: List @@ -140,7 +140,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="174" + ```sds linenums="191" @Pure @PythonMacro("len($this)") fun size() -> size: Int @@ -171,7 +171,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="208" + ```sds linenums="225" @Pure @PythonMacro("list($this.values())") fun values() -> values: List diff --git a/docs/api/safeds/lang/Nothing.md b/docs/api/safeds/lang/Nothing.md index 7f94f741c..884749d90 100644 --- a/docs/api/safeds/lang/Nothing.md +++ b/docs/api/safeds/lang/Nothing.md @@ -9,6 +9,6 @@ The common subclass of all classes. ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="24" + ```sds linenums="41" class Nothing ``` diff --git a/docs/api/safeds/lang/Number.md b/docs/api/safeds/lang/Number.md index d3223da96..a33a2d9f6 100644 --- a/docs/api/safeds/lang/Number.md +++ b/docs/api/safeds/lang/Number.md @@ -14,6 +14,6 @@ A number. ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="44" + ```sds linenums="61" class Number ``` diff --git a/docs/api/safeds/lang/String.md b/docs/api/safeds/lang/String.md index 48633fa79..674bcd379 100644 --- a/docs/api/safeds/lang/String.md +++ b/docs/api/safeds/lang/String.md @@ -17,7 +17,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="221" + ```sds linenums="238" class String { /** @@ -296,7 +296,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="243" + ```sds linenums="260" @Pure @PythonMacro("$substring in $this") fun contains(substring: String) -> contains: Boolean @@ -328,7 +328,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="255" + ```sds linenums="272" @Pure @PythonMacro("$this.endswith($suffix)") fun endsWith(suffix: String) -> endsWith: Boolean @@ -360,7 +360,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="294" + ```sds linenums="311" @Pure @PythonMacro("$this.find($substring)") fun indexOf(substring: String) -> index: Int @@ -392,7 +392,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="306" + ```sds linenums="323" @Pure @PythonMacro("$this.rfind($substring)") fun lastIndexOf(substring: String) -> index: Int @@ -418,7 +418,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="231" + ```sds linenums="248" @Pure @PythonMacro("len($this)") fun length() -> length: Int @@ -450,7 +450,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="330" + ```sds linenums="347" @Pure @PythonMacro("$this * $n") fun repeat(n: Int) -> repeatedString: String @@ -483,7 +483,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="318" + ```sds linenums="335" @Pure @PythonMacro("$this.replace($old, $new)") fun replace(old: String, new: String) -> replacedString: String @@ -515,7 +515,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="342" + ```sds linenums="359" @Pure @PythonMacro("$this.split($separator)") fun split(separator: String) -> parts: List @@ -547,7 +547,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="267" + ```sds linenums="284" @Pure @PythonMacro("$this.startswith($prefix)") fun startsWith(prefix: String) -> startsWith: Boolean @@ -580,7 +580,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="282" + ```sds linenums="299" @Pure @PythonMacro("$this[$start:$end]") fun substring(start: Int = 0, end: Int = this.length()) -> substring: String @@ -614,7 +614,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="398" + ```sds linenums="415" @Pure @PythonMacro("$this.casefold()") fun toCasefolded() -> casefolded: String @@ -640,7 +640,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="448" + ```sds linenums="465" @Pure @PythonMacro("float($this)") fun toFloat() -> float: Float @@ -677,7 +677,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="467" + ```sds linenums="484" @Pure @PythonMacro("int($this, $base)") fun toInt(base: Int = 10) -> int: Int @@ -710,7 +710,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="417" + ```sds linenums="434" @Pure @PythonMacro("$this.lower()") fun toLowercase() -> lowercase: String @@ -743,7 +743,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="436" + ```sds linenums="453" @Pure @PythonMacro("$this.upper()") fun toUppercase() -> uppercase: String @@ -769,7 +769,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="354" + ```sds linenums="371" @Pure @PythonMacro("$this.strip()") fun trim() -> trimmed: String @@ -795,7 +795,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="378" + ```sds linenums="395" @Pure @PythonMacro("$this.rstrip()") fun trimEnd() -> trimmed: String @@ -821,7 +821,7 @@ pipeline example { ??? quote "Stub code in `coreClasses.sdsstub`" - ```sds linenums="366" + ```sds linenums="383" @Pure @PythonMacro("$this.lstrip()") fun trimStart() -> trimmed: String diff --git a/packages/safe-ds-lang/src/resources/builtins/safeds/lang/coreClasses.sdsstub b/packages/safe-ds-lang/src/resources/builtins/safeds/lang/coreClasses.sdsstub index 0bd40bce5..f08cf169b 100644 --- a/packages/safe-ds-lang/src/resources/builtins/safeds/lang/coreClasses.sdsstub +++ b/packages/safe-ds-lang/src/resources/builtins/safeds/lang/coreClasses.sdsstub @@ -5,6 +5,23 @@ package safeds.lang */ class Any { + /** + * Return whether the object is truthy. + * + * @example + * pipeline example { + * val boolean = 1.toBoolean(); // true + * } + * + * @example + * pipeline example { + * val boolean = 0.toBoolean(); // false + * } + */ + @Pure + @PythonMacro("bool($this)") + fun toBoolean() -> boolean: Boolean + /** * Return a string representation of the object. * diff --git a/packages/safe-ds-lang/tests/resources/generation/python/macros/any/input.sdsdev b/packages/safe-ds-lang/tests/resources/generation/python/macros/any/input.sdsdev new file mode 100644 index 000000000..2dd230f62 --- /dev/null +++ b/packages/safe-ds-lang/tests/resources/generation/python/macros/any/input.sdsdev @@ -0,0 +1,6 @@ +package tests.generation.python.macros.strings + +pipeline myPipeline { + val toBoolean = "Hello, world!".toBoolean(); + val toString = "Hello, world!".toString(); +}