Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'

like image 425
Martin Erlic Avatar asked May 01 '17 15:05

Martin Erlic


People also ask

What does != Mean in Kotlin?

== operator in Kotlin only compares the data or variables, whereas in Java or other languages == is generally used to compare the references. The negated counterpart of == in Kotlin is != which is used to compare if both the values are not equal to each other.

How do you declare an int variable in Kotlin?

Kotlin uses two different keywords to declare variables: val and var . Use val for a variable whose value never changes. You can't reassign a value to a variable that was declared using val . Use var for a variable whose value can change.

What is long in Kotlin?

Long is a 64-bit number in Kotlin The type can be either declared explicitly or the compiler itself has the ability to infer the type of the assigned value. The long data type can have values from -(2^63) to (2^63)-1.


2 Answers

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.

like image 67
Francesc Avatar answered Sep 17 '22 12:09

Francesc


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" 
like image 24
cfl Avatar answered Sep 20 '22 12:09

cfl