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;
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
add a comment |
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
What happened to the answer usingwhen
? 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
add a comment |
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
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
kotlin
asked Mar 22 at 7:48
JohnJohn
184114
184114
What happened to the answer usingwhen
? 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
add a comment |
What happened to the answer usingwhen
? 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
add a comment |
2 Answers
2
active
oldest
votes
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.
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 anelse if
, but this could have been perfectly explained/solved as a comment...
– Roland
Mar 22 at 10:54
add a comment |
You can use setOf
:
if (people.job in setOf(Job.Developer, Job.Scientist))
println("Fine")
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. Ifpeople.job in setOf(Job.Developer, Job.Scientist)
worth replacing withpeople.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, butsetOf
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 thansetOf
, not really less readable.
– Alexey Romanov
Mar 22 at 18:18
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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 anelse if
, but this could have been perfectly explained/solved as a comment...
– Roland
Mar 22 at 10:54
add a comment |
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.
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 anelse if
, but this could have been perfectly explained/solved as a comment...
– Roland
Mar 22 at 10:54
add a comment |
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.
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.
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 anelse if
, but this could have been perfectly explained/solved as a comment...
– Roland
Mar 22 at 10:54
add a comment |
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 anelse 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
add a comment |
You can use setOf
:
if (people.job in setOf(Job.Developer, Job.Scientist))
println("Fine")
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. Ifpeople.job in setOf(Job.Developer, Job.Scientist)
worth replacing withpeople.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, butsetOf
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 thansetOf
, not really less readable.
– Alexey Romanov
Mar 22 at 18:18
add a comment |
You can use setOf
:
if (people.job in setOf(Job.Developer, Job.Scientist))
println("Fine")
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. Ifpeople.job in setOf(Job.Developer, Job.Scientist)
worth replacing withpeople.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, butsetOf
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 thansetOf
, not really less readable.
– Alexey Romanov
Mar 22 at 18:18
add a comment |
You can use setOf
:
if (people.job in setOf(Job.Developer, Job.Scientist))
println("Fine")
You can use setOf
:
if (people.job in setOf(Job.Developer, Job.Scientist))
println("Fine")
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. Ifpeople.job in setOf(Job.Developer, Job.Scientist)
worth replacing withpeople.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, butsetOf
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 thansetOf
, not really less readable.
– Alexey Romanov
Mar 22 at 18:18
add a comment |
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. Ifpeople.job in setOf(Job.Developer, Job.Scientist)
worth replacing withpeople.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, butsetOf
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 thansetOf
, 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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