# Introduce intermediate interface for integer numbers

**URL:** <https://discuss.kotlinlang.org/t/introduce-intermediate-interface-for-integer-numbers/17927>\
**Category:** Language Design\
**Created:** [June 2, 2020, 2:13pm UTC](https://discuss.kotlinlang.org/t/introduce-intermediate-interface-for-integer-numbers/17927 "2020-06-02T14:13:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sollecitom](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sollecitom/32/3552_2.png) [@sollecitom](https://discuss.kotlinlang.org/u/sollecitom)\
**Post date:** [June 2, 2020, 2:13pm UTC](https://discuss.kotlinlang.org/t/introduce-intermediate-interface-for-integer-numbers/17927/1 "2020-06-02T14:13:14Z")

</div>

It’d be useful to have an intermediate interface that integer number types could implement, like Int, Long and BigInteger. On the dual side, a common interface for floating point number types would also be useful, for Float, Double, BigDecimal, etc.

This would allow the compiler to make my life easier when I need an arbitrary length for a number, but I want a specific category.

Now I’m forced to overload some declarations, like

```auto
val Int.pennies: Pounds get() = Pounds(this.toBigInteger())
val Long.pennies: Pounds get() = Pounds(this.toBigInteger())
val BigInteger.pennies: Pounds get() = Pounds(pennies = this)

```

and it’d be great to be able to write instead

```auto
val IntegerNumber.pennies: Pounds = Pounds(this)

```

There are many more use cases for both integer number and floating point numbers.

---

<div class="post-metadata">

**Author:** ![imanushin](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/imanushin/32/5471_2.png) [@imanushin](https://discuss.kotlinlang.org/u/imanushin)\
**Post date:** [June 2, 2020, 6:03pm UTC](https://discuss.kotlinlang.org/t/introduce-intermediate-interface-for-integer-numbers/17927/2 "2020-06-02T18:03:30Z")

</div>

Probably you are talking about the [shape classes](https://github.com/dotnet/csharplang/issues/164).

Theoretically, they could be implemented as extensions for `Long.Companion` types.

---

<div class="post-metadata">

**Author:** ![sollecitom](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sollecitom/32/3552_2.png) [@sollecitom](https://discuss.kotlinlang.org/u/sollecitom)\
**Post date:** [June 2, 2020, 8:50pm UTC](https://discuss.kotlinlang.org/t/introduce-intermediate-interface-for-integer-numbers/17927/3 "2020-06-02T20:50:22Z")

</div>

I mean, potentially yes, but not necessarily. The issue with shape classes is that they’re not types, and as such they’re like pattern matching for generics, which is a great idea, but only half the deal. Here it’d be great to be able to accept a number without a floating part as a type, and have the compiler help you with that.

If this improvement is not deemed worthy enough of an ad-hoc solution, an alternative general way of solving this would be introducing union and intersection types.

So in this case I’d be able to write:

```auto
val Int|Long|BigInteger.pounds: Pounds get() = Pounds(this)

class Pounds(val amount: Int|Long|BigInteger)

```

or better, also with importable type aliases:

```auto
typealias IntegerNumber = Int|Long|BigInteger

val IntegerNumber.pounds: Pounds get() = Pounds(this)

class Pounds(val amount: IntegerNumber)

```

These 2 features, union & intersection types and importable type aliases would be a killer together, and can be used in so many ways. Also play great with dynamic casting, like:

```auto
val something: Float|Boolean|Double

if (something !is Boolean) {
   println(something.toBigDecimal())
} else {
  println(!something)
}

```
