Kotlin how to check if something is either a or Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceKotlin Ternary Conditional OperatorHow to convert String to Long in Kotlin?Extend data class in KotlinHow to initialize an array in Kotlin with values?How to convert List to Map in Kotlin?What is the smartest way to copy a Map in Kotlin?Kotlin - Property initialization using “by lazy” vs. “lateinit”What is the equivalent of Java static methods in Kotlin?Val and Var in KotlinKotlin nullibility check if-else functional approach…How?

What items from the Roman-age tech-level could be used to deter all creatures from entering a small area?

Can a monk deflect thrown melee weapons?

Is it possible to ask for a hotel room without minibar/extra services?

Direct Experience of Meditation

What to do with post with dry rot?

Is above average number of years spent on PhD considered a red flag in future academia or industry positions?

Can a zero nonce be safely used with AES-GCM if the key is random and never used again?

What computer would be fastest for Mathematica Home Edition?

How to rotate it perfectly?

Writing Thesis: Copying from published papers

If A makes B more likely then B makes A more likely"

New Order #5: where Fibonacci and Beatty meet at Wythoff

Typsetting diagram chases (with TikZ?)

What did Darwin mean by 'squib' here?

What's the point in a preamp?

How can you insert a "times/divide" symbol similar to the "plus/minus" (±) one?

3 doors, three guards, one stone

Is drag coefficient lowest at zero angle of attack?

Can a non-EU citizen traveling with me come with me through the EU passport line?

Cauchy Sequence Characterized only By Directly Neighbouring Sequence Members

What is the electric potential inside a point charge?

When communicating altitude with a '9' in it, should it be pronounced "nine hundred" or "niner hundred"?

How to set letter above or below the symbol?

Single author papers against my advisor's will?



Kotlin how to check if something is either a or



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceKotlin Ternary Conditional OperatorHow to convert String to Long in Kotlin?Extend data class in KotlinHow to initialize an array in Kotlin with values?How to convert List to Map in Kotlin?What is the smartest way to copy a Map in Kotlin?Kotlin - Property initialization using “by lazy” vs. “lateinit”What is the equivalent of Java static methods in Kotlin?Val and Var in KotlinKotlin nullibility check if-else functional approach…How?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I want to check



if (people.job == Job.Developer || people.job == Job.Scientist) 
// do Something



However, it looks ugly to me.



I want something like:



if (people.job in (Job.Developer, Job.Scientist)) 










share|improve this question






















  • What happened to the answer using when?  That seemed like a good approach to me.

    – gidds
    Mar 22 at 9:59











  • @gidds it was deleted by Martin after someone downvoted it... what a pity... I still find it better then to create a list or set...

    – Roland
    Mar 22 at 10:43

















0















I want to check



if (people.job == Job.Developer || people.job == Job.Scientist) 
// do Something



However, it looks ugly to me.



I want something like:



if (people.job in (Job.Developer, Job.Scientist)) 










share|improve this question






















  • What happened to the answer using when?  That seemed like a good approach to me.

    – gidds
    Mar 22 at 9:59











  • @gidds it was deleted by Martin after someone downvoted it... what a pity... I still find it better then to create a list or set...

    – Roland
    Mar 22 at 10:43













0












0








0








I want to check



if (people.job == Job.Developer || people.job == Job.Scientist) 
// do Something



However, it looks ugly to me.



I want something like:



if (people.job in (Job.Developer, Job.Scientist)) 










share|improve this question














I want to check



if (people.job == Job.Developer || people.job == Job.Scientist) 
// do Something



However, it looks ugly to me.



I want something like:



if (people.job in (Job.Developer, Job.Scientist)) 







kotlin






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 22 at 7:48









JohnJohn

184114




184114












  • What happened to the answer using when?  That seemed like a good approach to me.

    – gidds
    Mar 22 at 9:59











  • @gidds it was deleted by Martin after someone downvoted it... what a pity... I still find it better then to create a list or set...

    – Roland
    Mar 22 at 10:43

















  • What happened to the answer using when?  That seemed like a good approach to me.

    – gidds
    Mar 22 at 9:59











  • @gidds it was deleted by Martin after someone downvoted it... what a pity... I still find it better then to create a list or set...

    – Roland
    Mar 22 at 10:43
















What happened to the answer using when?  That seemed like a good approach to me.

– gidds
Mar 22 at 9:59





What happened to the answer using when?  That seemed like a good approach to me.

– gidds
Mar 22 at 9:59













@gidds it was deleted by Martin after someone downvoted it... what a pity... I still find it better then to create a list or set...

– Roland
Mar 22 at 10:43





@gidds it was deleted by Martin after someone downvoted it... what a pity... I still find it better then to create a list or set...

– Roland
Mar 22 at 10:43












2 Answers
2






active

oldest

votes


















4














Unfortunately the answer of Martin L. Jensen was downvoted and deleted by himself, but I think when itself is actually still the easiest way to solve this:



when (people.job) 
Job.Developer, Job.Scientist -> println("Fine") // or return a value or whatever



The benefit in contrast to other shown solutions: no intermediate Set or List is created which you discard immediately after.






share|improve this answer

























  • I can't see Martin's answer, but if that was his answer I'd like to know why it was downvoted. I can't see any drawbacks

    – user2340612
    Mar 22 at 10:52






  • 1





    @user2340612 it isn't exactly the same... his answer was actually doing an else if, but this could have been perfectly explained/solved as a comment...

    – Roland
    Mar 22 at 10:54


















3














You can use setOf:



if (people.job in setOf(Job.Developer, Job.Scientist)) 
println("Fine")






share|improve this answer


















  • 3





    Worth pointing out the performance implications of that approach: it creates a new set (which can be quite big), and does a lot more processing than simple comparisons.

    – gidds
    Mar 22 at 8:05











  • True, but: 1) we cannot really say how it affects application performance without benchmarking (I do not think it will be noticeable), 2) set creation could be moved into a separate variable and most likely it will be reused somewhere else in the application.

    – awesoon
    Mar 22 at 8:13











  • Reusing the set would reduce the object-creation and heap-churn overhead, but it would still need extra processing (calculation of hashcodes &c) and memory access.  And that would use more code than a simple pair of comparisons!  — As you say, the overall impact will depend very much on the application (how often this check is needed, and what else it's doing), but for me it would probably outweigh the slight code advantage unless there were a lot more than two comparisons.

    – gidds
    Mar 22 at 9:57











  • Probably a matter of taste. If people.job in setOf(Job.Developer, Job.Scientist) worth replacing with people.job == Job.Developer || people.job == Job.Scientist (in terms of performance) then there is either something terribly wrong with the application or the only thing the application can do is to check if a user job is developer or scientist. I am fine with both options, but setOf is more natural, readable and extendable.

    – awesoon
    Mar 22 at 10:33











  • arrayOf could be a compromise: faster to create, and for small number of alternatives likely faster to check than setOf, not really less readable.

    – Alexey Romanov
    Mar 22 at 18:18












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55295031%2fkotlin-how-to-check-if-something-is-either-a-or%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














Unfortunately the answer of Martin L. Jensen was downvoted and deleted by himself, but I think when itself is actually still the easiest way to solve this:



when (people.job) 
Job.Developer, Job.Scientist -> println("Fine") // or return a value or whatever



The benefit in contrast to other shown solutions: no intermediate Set or List is created which you discard immediately after.






share|improve this answer

























  • I can't see Martin's answer, but if that was his answer I'd like to know why it was downvoted. I can't see any drawbacks

    – user2340612
    Mar 22 at 10:52






  • 1





    @user2340612 it isn't exactly the same... his answer was actually doing an else if, but this could have been perfectly explained/solved as a comment...

    – Roland
    Mar 22 at 10:54















4














Unfortunately the answer of Martin L. Jensen was downvoted and deleted by himself, but I think when itself is actually still the easiest way to solve this:



when (people.job) 
Job.Developer, Job.Scientist -> println("Fine") // or return a value or whatever



The benefit in contrast to other shown solutions: no intermediate Set or List is created which you discard immediately after.






share|improve this answer

























  • I can't see Martin's answer, but if that was his answer I'd like to know why it was downvoted. I can't see any drawbacks

    – user2340612
    Mar 22 at 10:52






  • 1





    @user2340612 it isn't exactly the same... his answer was actually doing an else if, but this could have been perfectly explained/solved as a comment...

    – Roland
    Mar 22 at 10:54













4












4








4







Unfortunately the answer of Martin L. Jensen was downvoted and deleted by himself, but I think when itself is actually still the easiest way to solve this:



when (people.job) 
Job.Developer, Job.Scientist -> println("Fine") // or return a value or whatever



The benefit in contrast to other shown solutions: no intermediate Set or List is created which you discard immediately after.






share|improve this answer















Unfortunately the answer of Martin L. Jensen was downvoted and deleted by himself, but I think when itself is actually still the easiest way to solve this:



when (people.job) 
Job.Developer, Job.Scientist -> println("Fine") // or return a value or whatever



The benefit in contrast to other shown solutions: no intermediate Set or List is created which you discard immediately after.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 22 at 11:02

























answered Mar 22 at 10:40









RolandRoland

10.6k11542




10.6k11542












  • I can't see Martin's answer, but if that was his answer I'd like to know why it was downvoted. I can't see any drawbacks

    – user2340612
    Mar 22 at 10:52






  • 1





    @user2340612 it isn't exactly the same... his answer was actually doing an else if, but this could have been perfectly explained/solved as a comment...

    – Roland
    Mar 22 at 10:54

















  • I can't see Martin's answer, but if that was his answer I'd like to know why it was downvoted. I can't see any drawbacks

    – user2340612
    Mar 22 at 10:52






  • 1





    @user2340612 it isn't exactly the same... his answer was actually doing an else if, but this could have been perfectly explained/solved as a comment...

    – Roland
    Mar 22 at 10:54
















I can't see Martin's answer, but if that was his answer I'd like to know why it was downvoted. I can't see any drawbacks

– user2340612
Mar 22 at 10:52





I can't see Martin's answer, but if that was his answer I'd like to know why it was downvoted. I can't see any drawbacks

– user2340612
Mar 22 at 10:52




1




1





@user2340612 it isn't exactly the same... his answer was actually doing an else if, but this could have been perfectly explained/solved as a comment...

– Roland
Mar 22 at 10:54





@user2340612 it isn't exactly the same... his answer was actually doing an else if, but this could have been perfectly explained/solved as a comment...

– Roland
Mar 22 at 10:54













3














You can use setOf:



if (people.job in setOf(Job.Developer, Job.Scientist)) 
println("Fine")






share|improve this answer


















  • 3





    Worth pointing out the performance implications of that approach: it creates a new set (which can be quite big), and does a lot more processing than simple comparisons.

    – gidds
    Mar 22 at 8:05











  • True, but: 1) we cannot really say how it affects application performance without benchmarking (I do not think it will be noticeable), 2) set creation could be moved into a separate variable and most likely it will be reused somewhere else in the application.

    – awesoon
    Mar 22 at 8:13











  • Reusing the set would reduce the object-creation and heap-churn overhead, but it would still need extra processing (calculation of hashcodes &c) and memory access.  And that would use more code than a simple pair of comparisons!  — As you say, the overall impact will depend very much on the application (how often this check is needed, and what else it's doing), but for me it would probably outweigh the slight code advantage unless there were a lot more than two comparisons.

    – gidds
    Mar 22 at 9:57











  • Probably a matter of taste. If people.job in setOf(Job.Developer, Job.Scientist) worth replacing with people.job == Job.Developer || people.job == Job.Scientist (in terms of performance) then there is either something terribly wrong with the application or the only thing the application can do is to check if a user job is developer or scientist. I am fine with both options, but setOf is more natural, readable and extendable.

    – awesoon
    Mar 22 at 10:33











  • arrayOf could be a compromise: faster to create, and for small number of alternatives likely faster to check than setOf, not really less readable.

    – Alexey Romanov
    Mar 22 at 18:18
















3














You can use setOf:



if (people.job in setOf(Job.Developer, Job.Scientist)) 
println("Fine")






share|improve this answer


















  • 3





    Worth pointing out the performance implications of that approach: it creates a new set (which can be quite big), and does a lot more processing than simple comparisons.

    – gidds
    Mar 22 at 8:05











  • True, but: 1) we cannot really say how it affects application performance without benchmarking (I do not think it will be noticeable), 2) set creation could be moved into a separate variable and most likely it will be reused somewhere else in the application.

    – awesoon
    Mar 22 at 8:13











  • Reusing the set would reduce the object-creation and heap-churn overhead, but it would still need extra processing (calculation of hashcodes &c) and memory access.  And that would use more code than a simple pair of comparisons!  — As you say, the overall impact will depend very much on the application (how often this check is needed, and what else it's doing), but for me it would probably outweigh the slight code advantage unless there were a lot more than two comparisons.

    – gidds
    Mar 22 at 9:57











  • Probably a matter of taste. If people.job in setOf(Job.Developer, Job.Scientist) worth replacing with people.job == Job.Developer || people.job == Job.Scientist (in terms of performance) then there is either something terribly wrong with the application or the only thing the application can do is to check if a user job is developer or scientist. I am fine with both options, but setOf is more natural, readable and extendable.

    – awesoon
    Mar 22 at 10:33











  • arrayOf could be a compromise: faster to create, and for small number of alternatives likely faster to check than setOf, not really less readable.

    – Alexey Romanov
    Mar 22 at 18:18














3












3








3







You can use setOf:



if (people.job in setOf(Job.Developer, Job.Scientist)) 
println("Fine")






share|improve this answer













You can use setOf:



if (people.job in setOf(Job.Developer, Job.Scientist)) 
println("Fine")







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 22 at 7:59









awesoonawesoon

21.2k44775




21.2k44775







  • 3





    Worth pointing out the performance implications of that approach: it creates a new set (which can be quite big), and does a lot more processing than simple comparisons.

    – gidds
    Mar 22 at 8:05











  • True, but: 1) we cannot really say how it affects application performance without benchmarking (I do not think it will be noticeable), 2) set creation could be moved into a separate variable and most likely it will be reused somewhere else in the application.

    – awesoon
    Mar 22 at 8:13











  • Reusing the set would reduce the object-creation and heap-churn overhead, but it would still need extra processing (calculation of hashcodes &c) and memory access.  And that would use more code than a simple pair of comparisons!  — As you say, the overall impact will depend very much on the application (how often this check is needed, and what else it's doing), but for me it would probably outweigh the slight code advantage unless there were a lot more than two comparisons.

    – gidds
    Mar 22 at 9:57











  • Probably a matter of taste. If people.job in setOf(Job.Developer, Job.Scientist) worth replacing with people.job == Job.Developer || people.job == Job.Scientist (in terms of performance) then there is either something terribly wrong with the application or the only thing the application can do is to check if a user job is developer or scientist. I am fine with both options, but setOf is more natural, readable and extendable.

    – awesoon
    Mar 22 at 10:33











  • arrayOf could be a compromise: faster to create, and for small number of alternatives likely faster to check than setOf, not really less readable.

    – Alexey Romanov
    Mar 22 at 18:18













  • 3





    Worth pointing out the performance implications of that approach: it creates a new set (which can be quite big), and does a lot more processing than simple comparisons.

    – gidds
    Mar 22 at 8:05











  • True, but: 1) we cannot really say how it affects application performance without benchmarking (I do not think it will be noticeable), 2) set creation could be moved into a separate variable and most likely it will be reused somewhere else in the application.

    – awesoon
    Mar 22 at 8:13











  • Reusing the set would reduce the object-creation and heap-churn overhead, but it would still need extra processing (calculation of hashcodes &c) and memory access.  And that would use more code than a simple pair of comparisons!  — As you say, the overall impact will depend very much on the application (how often this check is needed, and what else it's doing), but for me it would probably outweigh the slight code advantage unless there were a lot more than two comparisons.

    – gidds
    Mar 22 at 9:57











  • Probably a matter of taste. If people.job in setOf(Job.Developer, Job.Scientist) worth replacing with people.job == Job.Developer || people.job == Job.Scientist (in terms of performance) then there is either something terribly wrong with the application or the only thing the application can do is to check if a user job is developer or scientist. I am fine with both options, but setOf is more natural, readable and extendable.

    – awesoon
    Mar 22 at 10:33











  • arrayOf could be a compromise: faster to create, and for small number of alternatives likely faster to check than setOf, not really less readable.

    – Alexey Romanov
    Mar 22 at 18:18








3




3





Worth pointing out the performance implications of that approach: it creates a new set (which can be quite big), and does a lot more processing than simple comparisons.

– gidds
Mar 22 at 8:05





Worth pointing out the performance implications of that approach: it creates a new set (which can be quite big), and does a lot more processing than simple comparisons.

– gidds
Mar 22 at 8:05













True, but: 1) we cannot really say how it affects application performance without benchmarking (I do not think it will be noticeable), 2) set creation could be moved into a separate variable and most likely it will be reused somewhere else in the application.

– awesoon
Mar 22 at 8:13





True, but: 1) we cannot really say how it affects application performance without benchmarking (I do not think it will be noticeable), 2) set creation could be moved into a separate variable and most likely it will be reused somewhere else in the application.

– awesoon
Mar 22 at 8:13













Reusing the set would reduce the object-creation and heap-churn overhead, but it would still need extra processing (calculation of hashcodes &c) and memory access.  And that would use more code than a simple pair of comparisons!  — As you say, the overall impact will depend very much on the application (how often this check is needed, and what else it's doing), but for me it would probably outweigh the slight code advantage unless there were a lot more than two comparisons.

– gidds
Mar 22 at 9:57





Reusing the set would reduce the object-creation and heap-churn overhead, but it would still need extra processing (calculation of hashcodes &c) and memory access.  And that would use more code than a simple pair of comparisons!  — As you say, the overall impact will depend very much on the application (how often this check is needed, and what else it's doing), but for me it would probably outweigh the slight code advantage unless there were a lot more than two comparisons.

– gidds
Mar 22 at 9:57













Probably a matter of taste. If people.job in setOf(Job.Developer, Job.Scientist) worth replacing with people.job == Job.Developer || people.job == Job.Scientist (in terms of performance) then there is either something terribly wrong with the application or the only thing the application can do is to check if a user job is developer or scientist. I am fine with both options, but setOf is more natural, readable and extendable.

– awesoon
Mar 22 at 10:33





Probably a matter of taste. If people.job in setOf(Job.Developer, Job.Scientist) worth replacing with people.job == Job.Developer || people.job == Job.Scientist (in terms of performance) then there is either something terribly wrong with the application or the only thing the application can do is to check if a user job is developer or scientist. I am fine with both options, but setOf is more natural, readable and extendable.

– awesoon
Mar 22 at 10:33













arrayOf could be a compromise: faster to create, and for small number of alternatives likely faster to check than setOf, not really less readable.

– Alexey Romanov
Mar 22 at 18:18






arrayOf could be a compromise: faster to create, and for small number of alternatives likely faster to check than setOf, not really less readable.

– Alexey Romanov
Mar 22 at 18:18


















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55295031%2fkotlin-how-to-check-if-something-is-either-a-or%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript