Kotlin - Operators and ExpressionsHow can you use operator functions to make a custom class support the 'in' keyword in Kotlin?AOverride 'equals' function to check membershipBDefine operator function 'contains' that returns BooleanCDefine operator function 'in' that returns BooleanDUse operator function 'has' that returns BooleanCheck Answer
Step-by-Step SolutionSolution:Step 1: Recall 'in' keyword usageThe 'in' keyword calls the 'contains' operator function on the container object.Step 2: Define operator function 'contains'To support 'in', define 'operator fun contains(element): Boolean' in the class.Final Answer:Define operator function 'contains' that returns Boolean -> Option BQuick Check:'in' keyword calls 'contains' operator function [OK]Quick Trick: Use 'operator fun contains' to support 'in' keyword [OK]Common Mistakes:MISTAKESTrying to define operator named 'in'Using 'equals' for membership checkAssuming 'has' is valid operator function
Master "Operators and Expressions" in Kotlin9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Kotlin Quizzes Collections Fundamentals - List creation (listOf, mutableListOf) - Quiz 6medium Collections Fundamentals - List creation (listOf, mutableListOf) - Quiz 13medium Control Flow as Expressions - Smart casts in when and if - Quiz 11easy Control Flow as Expressions - When as expression returning value - Quiz 5medium Data Types - Nothing type for functions that never return - Quiz 2easy Functions - Function declaration syntax - Quiz 8hard Functions - Why functions are first-class in Kotlin - Quiz 4medium Kotlin Basics and JVM Runtime - Kotlin REPL and script mode - Quiz 2easy Null Safety - Nullable types with ? suffix - Quiz 6medium Operators and Expressions - Arithmetic operators - Quiz 8hard