Home > Mobile >  Unable to multiply due compile error in kotlin
Unable to multiply due compile error in kotlin

Time:07-26

Beginner question - Any idea why my code won't compile? I based it on a code given in this codelab and the given code compiles just fine. Any help is greatly appreciated :)

https://developer.android.com/codelabs/basic-android-kotlin-training-tip-calculator?continue=https://developer.android.com/courses/pathways/android-basics-kotlin-unit-2-pathway-1#codelab-https://developer.android.com/codelabs/basic-android-kotlin-training-tip-calculator#3

package com.example.converterapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.converterapp.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.calculateButton.setOnClickListener{ calculate() }
    }

    fun calculate() {
        val stringInTextField = binding.measurementToConvert.text.toString()
        val measurement = stringInTextField.toDoubleOrNull()
        if (measurement == null) {
            binding.convertedMeasurement.text = ""
            return
        }

        val selectedId = binding.conversionOptions.checkedRadioButtonId
        val conversion = when (selectedId) {
            R.id.oz_to_cups -> 0.125
            R.id.cups_to_oz -> 8
            R.id.oz_to_grams -> 28.3495
            else -> 0.035274
        }
        var desiredMeasurement = measurement * conversion
        val roundUp = binding.roundUpSwitch.isChecked
        if (roundUp) {
            desiredMeasurement = kotlin.math.ceil(desiredMeasurement)
        }
        binding.convertedMeasurement.text = getString(R.string.convertedMeasurement, desiredMeasurement)
    }

}

Error Message:

\ConverterApp\app\src\main\java\com\example\converterapp\MainActivity.kt: (33, 46): None of the following functions can be called with the arguments supplied: 
public final operator fun times(other: Byte): Double defined in kotlin.Double
public final operator fun times(other: Double): Double defined in kotlin.Double
public final operator fun times(other: Float): Double defined in kotlin.Double
public final operator fun times(other: Int): Double defined in kotlin.Double
public final operator fun times(other: Long): Double defined in kotlin.Double
public final operator fun times(other: Short): Double defined in kotlin.Double

CodePudding user response:

Your variable conversion is not of type Byte, Double, Float, Int, etc. You assigned your variable type like this:

        val conversion = when (selectedId) {
            R.id.oz_to_cups -> 0.125 // This is a Double
            R.id.cups_to_oz -> 8 // This is an Int
            R.id.oz_to_grams -> 28.3495
            else -> 0.035274
        }

Since one of your branches is an Int and the others are Doubles, and you are leaving the type implicit (didn't explicitly give it a type with : SomeType =), Kotlin chooses a type for the variable that is common to all branches. In this case, the types Int and Double have the types Number and Comparable in common since they both implement those interfaces, so it will choose one of those (I don't remember which).

Then you try to use multiplication of measurement (a Double) with either a Number or a Comparable, which is impossible, because that is not a defined times operator function for Double. Double can only be multiplied by one of the specific types of primitives, unless you define your own extension operator function that accepts other types like Number.

The simple fix for your code is to change 8 to 8.0 so it will be a Double. Then conversion will implicitly be a Double.

  • Related