Skip to content

Commit

Permalink
feat: make Int a subclass of Float (#1255)
Browse files Browse the repository at this point in the history
### Summary of Changes

`Int` is now a subclass of `Float`, which allows using int literals
where floats are expected.
  • Loading branch information
lars-reimann authored Nov 2, 2024
1 parent 0bb1df0 commit c8f3b67
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 37 deletions.
12 changes: 8 additions & 4 deletions docs/api/safeds/lang/Float.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ A floating-point number.

**Parent type:** [`Number`][safeds.lang.Number]

**Inheritors:**

- [`Int`][safeds.lang.Int]

**Examples:**

```sds
Expand All @@ -21,15 +25,15 @@ pipeline example {

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

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

/**
* Convert this floating-point number to an integer by truncating the fractional part.
*
* @example
* pipeline example {
* val int = 1.0.toInt(); // 1
* val int = (1.0).toInt(); // 1
* }
*/
@Pure
Expand All @@ -52,13 +56,13 @@ Convert this floating-point number to an integer by truncating the fractional pa

```sds hl_lines="2"
pipeline example {
val int = 1.0.toInt(); // 1
val int = (1.0).toInt(); // 1
}
```

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

```sds linenums="104"
```sds linenums="81"
@Pure
@PythonMacro("int($this)")
fun toInt() -> int: Int
Expand Down
34 changes: 30 additions & 4 deletions docs/api/safeds/lang/Int.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ search:

An integer.

**Parent type:** [`Number`][safeds.lang.Number]
**Parent type:** [`Float`][safeds.lang.Float]

**Examples:**

Expand All @@ -21,8 +21,8 @@ pipeline example {

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

```sds linenums="71"
class Int sub Number {
```sds linenums="94"
class Int sub Float {

/**
* Convert this integer to a floating-point number.
Expand Down Expand Up @@ -58,8 +58,34 @@ pipeline example {

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

```sds linenums="81"
```sds linenums="104"
@Pure
@PythonMacro("float($this)")
fun toFloat() -> float: Float
```

## <code class="doc-symbol doc-symbol-function"></code> `toInt` {#safeds.lang.Int.toInt data-toc-label='[function] toInt'}

Convert this floating-point number to an integer by truncating the fractional part.

**Results:**

| Name | Type | Description |
|------|------|-------------|
| `int` | [`Int`][safeds.lang.Int] | - |

**Examples:**

```sds hl_lines="2"
pipeline example {
val int = (1.0).toInt(); // 1
}
```

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

```sds linenums="81"
@Pure
@PythonMacro("int($this)")
fun toInt() -> int: Int
```
1 change: 0 additions & 1 deletion docs/api/safeds/lang/Number.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ A number.
**Inheritors:**

- [`Float`][safeds.lang.Float]
- [`Int`][safeds.lang.Int]

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,49 +61,49 @@ class Boolean
class Number

/**
* An integer.
* A floating-point number.
*
* @example
* pipeline example {
* val int = 1;
* val float = 1.0;
* }
*/
class Int sub Number {
class Float sub Number {

/**
* Convert this integer to a floating-point number.
* Convert this floating-point number to an integer by truncating the fractional part.
*
* @example
* pipeline example {
* val float = 1.toFloat(); // 1.0
* val int = (1.0).toInt(); // 1
* }
*/
@Pure
@PythonMacro("float($this)")
fun toFloat() -> float: Float
@PythonMacro("int($this)")
fun toInt() -> int: Int
}

/**
* A floating-point number.
* An integer.
*
* @example
* pipeline example {
* val float = 1.0;
* val int = 1;
* }
*/
class Float sub Number {
class Int sub Float {

/**
* Convert this floating-point number to an integer by truncating the fractional part.
* Convert this integer to a floating-point number.
*
* @example
* pipeline example {
* val int = 1.0.toInt(); // 1
* val float = 1.toFloat(); // 1.0
* }
*/
@Pure
@PythonMacro("int($this)")
fun toInt() -> int: Int
@PythonMacro("float($this)")
fun toFloat() -> float: Float
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const tests: ComputeClassTypeForLiteralTypeTest[] = [
},
{
literalType: factory.createLiteralType(new FloatConstant(1.5), new IntConstant(1n)),
expected: coreTypes.Number,
expected: coreTypes.Float,
},
{
literalType: factory.createLiteralType(new IntConstant(1n), new StringConstant('')),
Expand All @@ -78,7 +78,7 @@ const tests: ComputeClassTypeForLiteralTypeTest[] = [
},
{
literalType: factory.createLiteralType(new FloatConstant(1.5), new IntConstant(1n), NullConstant),
expected: coreTypes.Number.withExplicitNullability(true),
expected: coreTypes.Float.withExplicitNullability(true),
},
{
literalType: factory.createLiteralType(new IntConstant(1n), new StringConstant(''), NullConstant),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ search:

- safeds
- lang
- [Int](safeds/lang/Int.md)
- [Float](safeds/lang/Float.md)
- [Number](safeds/lang/Number.md)
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ search:

[//]: # (DO NOT EDIT THIS FILE DIRECTLY. Instead, edit the corresponding stub file and execute `npm run docs:api`.)

# <code class="doc-symbol doc-symbol-class"></code> `Int` {#safeds.lang.Int data-toc-label='[class] Int'}
# <code class="doc-symbol doc-symbol-class"></code> `Float` {#safeds.lang.Float data-toc-label='[class] Float'}

**Parent type:** `#!sds Number`

**Inheritors:**

- `#!sds Int`

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

```sds linenums="5"
class Int sub Number
class Float sub Number
```
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ search:
**Inheritors:**

- `#!sds Float`
- `#!sds Int`

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package safeds.lang

class Number

class Int sub Number
class Float sub Number
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pipeline myPipeline {
// $TEST$ serialization List<literal<1, 2, 3>>
»[1, 2, 3]«;

// $TEST$ serialization List<Number>
// $TEST$ serialization List<Float>
»[1, float(), 3]«;

// $TEST$ serialization List<Any>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pipeline myPipeline {
// $TEST$ serialization Map<Int, literal<1, 2, 3>>
»{1: 1, 2: 2, 3: 3}«;

// $TEST$ serialization Map<Number, Number>
// $TEST$ serialization Map<Float, Float>
»{1: 1, float(): float(), 3: 3}«;

// $TEST$ serialization Map<Any, Any>
Expand All @@ -20,6 +20,6 @@ pipeline myPipeline {
// $TEST$ serialization Map<Any?, Any?>
»{1: 1, string(): string(), null: null}«;

// $TEST$ serialization Map<Number, String?>
// $TEST$ serialization Map<Float, String?>
»{float(): string(), 1: null}«;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pipeline elvisWithNullableLeftOperand {
»intOrNull() ?: null«;
// $TEST$ serialization Int
»intOrNull() ?: 1«;
// $TEST$ serialization Number
// $TEST$ serialization Float
»intOrNull() ?: 1.0«;
// $TEST$ serialization Any
»intOrNull() ?: ""«;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ segment covariant(
»[coInt, coString]«;
// $TEST$ serialization List<Contravariant<Nothing>>
»[coInt, coFloat]«;
// $TEST$ serialization List<Contravariant<SomeCovariant<Nothing, Boolean>>>
// $TEST$ serialization List<Contravariant<SomeCovariant<Int, Boolean>>>
»[coInt, coFloat2]«;

// $TEST$ serialization List<Contravariant<Covariant<String>>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ segment covariant(
»[coInt, coInt]«;
// $TEST$ serialization List<Covariant<Any>>
»[coInt, coString]«;
// $TEST$ serialization List<Covariant<Number>>
// $TEST$ serialization List<Covariant<Float>>
»[coInt, coFloat]«;

// $TEST$ serialization List<Covariant<String>>
Expand Down Expand Up @@ -88,7 +88,7 @@ segment multiple(
) {
// $TEST$ serialization List<Multiple<Number, Int>>
»[multipleNumberInt, multipleNumberInt]«;
// $TEST$ serialization List<Multiple<Number, Number>>
// $TEST$ serialization List<Multiple<Number, Float>>
»[multipleNumberInt, multipleNumberFloat]«;

// $TEST$ serialization List<Multiple<Number, Float>>
Expand Down

0 comments on commit c8f3b67

Please sign in to comment.