# Type check and conversion

**URL:** <https://discuss.kotlinlang.org/t/type-check-and-conversion/19533>\
**Category:** Support\
**Created:** [October 16, 2020, 11:23pm UTC](https://discuss.kotlinlang.org/t/type-check-and-conversion/19533 "2020-10-16T23:23:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![geruggimarco](https://avatars.discourse-cdn.com/v4/letter/g/9fc348/32.png) [@geruggimarco](https://discuss.kotlinlang.org/u/geruggimarco)\
**Post date:** [October 16, 2020, 11:23pm UTC](https://discuss.kotlinlang.org/t/type-check-and-conversion/19533/1 "2020-10-16T23:23:50Z")

</div>

Hi!  
I’m a beginner in Kotlin programming and I’m trying to understand how the “if” function works.  
Following is a code I’m trying to fix, some suggestion and explanation from you?

```
    fun sumNumbers(num1: Double, num2: Double): Double{
    
    if(num1 || num2 is Int){
        num1.toInt()
        num2.toInt()
        
    }
    return num1 + num2
}

fun main(args:Array<String>) {
    
    var somma = sumNumbers(5, 34)
    print(somma)
    
    }
    }

```

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [October 17, 2020, 6:52am UTC](https://discuss.kotlinlang.org/t/type-check-and-conversion/19533/2 "2020-10-17T06:52:30Z")

</div>

`if` is not a function, it is a keyword that executes its block only if the condition inside of it is true.

> [@geruggimarco](#):
>
> `if(num1 || num2 is Int)`

This will never work cuz 1) you can’t have 2 conditions in one using `||`, so it’ll need to be `if(num1 is Int || num2 is Int)` 2) This is an `||`, but it seems as though you actually want a `&&` because you are converting both numbers and finally 3) a `Double` can never be cast as an `Int`, because they are totally different types. To be honest, I don’t really see the purpose of that function. Did you maybe just want to add 2 Ints? if so, then make `num1` and `num2` Ints. Could you please clarify what you are trying to achieve?

---

<div class="post-metadata">

**Author:** ![arocnies](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/arocnies/32/5143_2.png) [@arocnies](https://discuss.kotlinlang.org/u/arocnies)\
**Post date:** [October 17, 2020, 4:45pm UTC](https://discuss.kotlinlang.org/t/type-check-and-conversion/19533/3 "2020-10-17T16:45:14Z")

</div>

Here’s an example for a function summing two numbers:

```run-kotlin
fun sumNumbers(num1: Double, num2: Double): Double = num1 + num2

fun main() {
    val sum = sumNumbers(5.0, 34.0)
    println(sum)
}

```

Just as a reminder, when we say Integer and Double, we aren’t talking about numbers like we do learning math–instead we’re talking about **types**. It’s reasonable to say whole numbers and integers are a subset of rational numbers. An `Int` in programming is not a subtype of `Double`.

Checking if an `Double` is an `Int` is not checking if it’s a whole number. You might as well check if a `Double` is a `File` or `String`

```run-kotlin
import java.io.File
fun main() {
//sampleStart
5.0 is File
5.0 is String
5.0 is Int
//sampleEnd
}

```

If what you really want is to check if a `Double` is a whole number, you can do that a few different ways. Here’s an example using `compareTo`:

```run-kotlin

fun isWholeNumber(num: Double): Boolean {
    return num.compareTo(num.toInt()) == 0 
}

fun main() {
    val wholeDouble = 5.0
    println(isWholeNumber(wholeDouble))

    val nonWholeDouble = 5.0000001
    println(isWholeNumber(nonWholeDouble))
}

```

---

<div class="post-metadata">

**Author:** ![geruggimarco](https://avatars.discourse-cdn.com/v4/letter/g/9fc348/32.png) [@geruggimarco](https://discuss.kotlinlang.org/u/geruggimarco)\
**Post date:** [October 17, 2020, 5:46pm UTC](https://discuss.kotlinlang.org/t/type-check-and-conversion/19533/4 "2020-10-17T17:46:42Z")

</div>

so, how can I code a function that in case of the value is double returns double and in case of the value is integer returns an integer?

---

<div class="post-metadata">

**Author:** ![arocnies](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/arocnies/32/5143_2.png) [@arocnies](https://discuss.kotlinlang.org/u/arocnies)\
**Post date:** [October 17, 2020, 6:35pm UTC](https://discuss.kotlinlang.org/t/type-check-and-conversion/19533/5 "2020-10-17T18:35:13Z")

</div>

You have a few options but you probably want to define two functions with the same name (aka function overloading)

```kotlin
fun sumNumbers(num1: Int, num2: Int): Int = num1 + num2
fun sumNumbers(num1: Double, num2: Double): Double = num1 + num2

```

You can use the `Number` supertype if you want to support anything that can be converted to the basic number types.

```kotlin
fun sumNumbers(num1: Number, num2: Number): Double {
    return num1.toDouble() + num2.toDouble()
}

```

Docs for basic number types: [https://kotlinlang.org/docs/reference/basic-types.html](https://kotlinlang.org/docs/reference/basic-types.html)

* * *

> [@geruggimarco](#):
>
> so, how can I code a function that in case of the value is double returns double and in case of the value is integer returns an integer?

You can also use generic function to do this.

```kotlin
// The return type must be the same as param type. Anything can be used for the T type.
fun <T> exampleGenericFunction(param: T): T {
   return TODO("Return some type of T")
}

```

Here’s the reference page on generics: [https://kotlinlang.org/docs/reference/generics.html](https://kotlinlang.org/docs/reference/generics.html)

EDIT: Removed some extra stuff as it’s probably misleading for this kind of simple stuff.

---

<div class="post-metadata">

**Author:** ![geruggimarco](https://avatars.discourse-cdn.com/v4/letter/g/9fc348/32.png) [@geruggimarco](https://discuss.kotlinlang.org/u/geruggimarco)\
**Post date:** [October 17, 2020, 11:37pm UTC](https://discuss.kotlinlang.org/t/type-check-and-conversion/19533/6 "2020-10-17T23:37:44Z")

</div>

Thanks so much, very clear and helpful answer!!!
