Bird
0
0

How can you use operator functions to make a custom class support the 'in' keyword in Kotlin?

hard📝 Application Q8 of 15
Kotlin - Operators and Expressions
How can you use operator functions to make a custom class support the 'in' keyword in Kotlin?
AOverride 'equals' function to check membership
BDefine operator function 'contains' that returns Boolean
CDefine operator function 'in' that returns Boolean
DUse operator function 'has' that returns Boolean
Step-by-Step Solution
Solution:
  1. Step 1: Recall 'in' keyword usage

    The 'in' keyword calls the 'contains' operator function on the container object.
  2. Step 2: Define operator function 'contains'

    To support 'in', define 'operator fun contains(element): Boolean' in the class.
  3. Final Answer:

    Define operator function 'contains' that returns Boolean -> Option B
  4. Quick Check:

    'in' keyword calls 'contains' operator function [OK]
Quick Trick: Use 'operator fun contains' to support 'in' keyword [OK]
Common Mistakes:
MISTAKES
  • Trying to define operator named 'in'
  • Using 'equals' for membership check
  • Assuming 'has' is valid operator function

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes