Operator == cannot be applied to 'Long' and 'Int' in Kotlin

I'm attempting to implement parts of Mike Penz' NavigationDrawer (https://github.com/mikepenz/MaterialDrawer) in Kotlin. Since then I've run into only a few issues, primarily with operators. Here is part of the code to instantiate the drawer itself. Android Studio doesn't throw any errors except where I'm using the == operator on int and Long variables:

        // Create the Drawer
        result = DrawerBuilder()
                .withSliderBackgroundColor(ContextCompat.getColor(applicationContext, R.color.top_header))
                .withActivity(this)
                .withToolbar(toolbar)
                .withHasStableIds(true)
                .withItemAnimator(AlphaCrossFadeAnimator())
                .withAccountHeader(headerResult!!)
                .addDrawerItems(
                        PrimaryDrawerItem().withName(R.string.drawer_item_profile).withIcon(FontAwesome.Icon.faw_user).withIdentifier(1).withSelectable(false).withIconColor(ContextCompat.getColor(applicationContext, R.color.icon_grey)).withTextColor(ContextCompat.getColor(applicationContext, R.color.stroke)),
                        PrimaryDrawerItem().withName(R.string.drawer_item_create).withIcon(FontAwesome.Icon.faw_paint_brush).withIdentifier(2).withSelectable(false).withIconColor(ContextCompat.getColor(applicationContext, R.color.icon_grey)).withTextColor(ContextCompat.getColor(applicationContext, R.color.stroke)),
                        PrimaryDrawerItem().withName(R.string.drawer_item_yaanich_news).withIcon(FontAwesome.Icon.faw_newspaper_o).withIdentifier(3).withSelectable(false).withIconColor(ContextCompat.getColor(applicationContext, R.color.icon_grey)).withTextColor(ContextCompat.getColor(applicationContext, R.color.stroke)),
                        PrimaryDrawerItem().withName(R.string.drawer_item_my_groups).withIcon(FontAwesome.Icon.faw_users).withIdentifier(4).withSelectable(false).withIconColor(ContextCompat.getColor(applicationContext, R.color.icon_grey)).withTextColor(ContextCompat.getColor(applicationContext, R.color.stroke)),
                        PrimaryDrawerItem().withName(R.string.drawer_item_settings).withIcon(FontAwesome.Icon.faw_cog).withIdentifier(5).withSelectable(false).withIconColor(ContextCompat.getColor(applicationContext, R.color.icon_grey)).withTextColor(ContextCompat.getColor(applicationContext, R.color.stroke))
                )
                .withOnDrawerItemClickListener { view, position, drawerItem ->

                    if (drawerItem != null) {
                        var intent: Intent? = null
                        if (drawerItem.identifier == (1) {
                            intent = Intent(this, UserProfileActivity::class.java)
                        } else if (drawerItem.identifier == 2) {
                            intent = Intent(this, YeetActivity::class.java)
                        } else if (drawerItem.identifier == 3) {
                            intent = Intent(this, RssActivity::class.java)
                        } else if (drawerItem.identifier == 4) {
                            intent = Intent(this, GroupsActivity::class.java)
                        } else if (drawerItem.identifier == 5) {
                            intent = Intent(this, UserSettingsActivity::class.java)
                        }
                        if (intent != null) {
                            this.startActivity(intent)
                        }
                    }
                    false
                }
                .withSavedInstance(savedInstanceState)
                .withShowDrawerOnFirstLaunch(true)
                .build()

        RecyclerViewCacheUtil<IDrawerItem<*, *>>().withCacheSize(2).apply(result!!.recyclerView, result!!.drawerItems)

        if (savedInstanceState == null) {
            result!!.setSelection(21, false)
            headerResult!!.activeProfile = profile
        }
    }

Errors:

if (drawerItem.identifier == (1)

if (drawerItem.identifier == 2)

Operator == cannot be applied to 'Long and' 'Int'


Solution 1:

Simply use long on your right side

if (drawerItem.identifier == 1L)

Edit: the reason this is required is that Kotlin does not promote Ints to Longs (or, more generally, does not widen types); on the left side we had a Long and on the right side we had an Int, which lead to the error. Explicitly indicating that the right side is a Long fixes the error.

Solution 2:

For interest, another solution would be to use compareTo(). compareTo returns zero if the values are equal, negative if its less than the other, or positive if its greater than the other:

 if(drawerItem.identifier.compareTo(1) == 0)   "Equals"

Solution 3:

If the above solution didn't work for you, try explicitly converting the value on right to the particular wider type on left side

num = 5.7
if (num == 4.toDouble()) {
    //some code
}

Every number type supports the following conversions:

  • toByte(): Byte
  • toShort(): Short
  • toInt(): Int
  • toLong(): Long
  • toFloat(): Float
  • toDouble(): Double
  • toChar(): Char