Skip to content

Commit

Permalink
feat: Check truthiness of value (#1131)
Browse files Browse the repository at this point in the history
### Summary of Changes

Add a method `toBoolean` to check the truthiness of any value.
  • Loading branch information
lars-reimann authored May 2, 2024
1 parent 6f7100d commit 0b059a1
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 35 deletions.
50 changes: 49 additions & 1 deletion docs/api/safeds/lang/Any.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/api/safeds/lang/Boolean.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ pipeline example {

??? quote "Stub code in `coreClasses.sdsstub`"

```sds linenums="39"
```sds linenums="56"
class Boolean
```
4 changes: 2 additions & 2 deletions docs/api/safeds/lang/Float.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pipeline example {

??? quote "Stub code in `coreClasses.sdsstub`"

```sds linenums="77"
```sds linenums="94"
class Float sub Number {

/**
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions docs/api/safeds/lang/Int.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pipeline example {

??? quote "Stub code in `coreClasses.sdsstub`"

```sds linenums="54"
```sds linenums="71"
class Int sub Number {

/**
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions docs/api/safeds/lang/List.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pipeline example {

??? quote "Stub code in `coreClasses.sdsstub`"

```sds linenums="100"
```sds linenums="117"
class List<out E> {

/**
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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<E>
Expand Down
8 changes: 4 additions & 4 deletions docs/api/safeds/lang/Map.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pipeline example {

??? quote "Stub code in `coreClasses.sdsstub`"

```sds linenums="159"
```sds linenums="176"
class Map<K, out V> {

/**
Expand Down Expand Up @@ -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<K>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<V>
Expand Down
2 changes: 1 addition & 1 deletion docs/api/safeds/lang/Nothing.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ The common subclass of all classes.

??? quote "Stub code in `coreClasses.sdsstub`"

```sds linenums="24"
```sds linenums="41"
class Nothing
```
2 changes: 1 addition & 1 deletion docs/api/safeds/lang/Number.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ A number.

??? quote "Stub code in `coreClasses.sdsstub`"

```sds linenums="44"
```sds linenums="61"
class Number
```
38 changes: 19 additions & 19 deletions docs/api/safeds/lang/String.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pipeline example {

??? quote "Stub code in `coreClasses.sdsstub`"

```sds linenums="221"
```sds linenums="238"
class String {

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<String>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Loading

0 comments on commit 0b059a1

Please sign in to comment.