C#-like case when in kotlin? - kotlin

I come from a C# background and one really nice thing they offer is a way to add conditionals to a switch statement
switch(type)
{
case "edit":
// do something...
break;
case "delete" when user.isAdmin:
// do something...
break;
}
I'm curious if Kotlin has such an ability with their equivalent when? I'm not seeing that it's possible but I'm also still getting the hang of Kotlin so maybe there is a way.

I figured it out, guess I should have just asked Android studio XD
for those wondering, heres how:
when {
type == "edit" -> {
// do sometihng...
}
type == "delete" && user.isAdmin -> {
// do sometihng...
}
}
hope this helped someone else.

Related

How to use IF / ELSE in a method of Vue

Issue: This method is supposed to accomplish the following:
IF the user is NOT in a specific room already found inside joinedRooms then join them and make the room tab above active. (this part works fine as shown below as the if statement)
ELSE (which means the user is already joined to said room), then just take them to the active tab above.
Here is where I am confused. The code below in my eyes SHOULD work. However, it isnt, it seems to always run the IF statement regardless. What is the incorrect syntax being used.
methods: {
joinRoom(room) {
if (room != this.$store.state.rooms.joinedRooms) {
this.$store.dispatch(RoomTypes.JOIN_ROOM, room);
this.$store.dispatch(RoomTypes.CURRENT_ROOM, room);
this.$emit('onClose')
}
else if (room === this.$store.state.rooms.joinedRooms ) {
this.$emit('onClose')
}
}
},
EDIT: What I am realizing thanks to a comment below is I need to check if the room is part of the array joinedRooms How would I accomplish this
Thanks to #CherryDT, I was able to realize I needed to use includes as part of the if statement. The code should look as follows
methods: {
joinRoom(room) {
if (!this.$store.state.rooms.joinedRooms.includes(room)) {
this.$store.dispatch(RoomTypes.JOIN_ROOM, room);
this.$store.dispatch(RoomTypes.CURRENT_ROOM, room);
this.$emit('onClose')
}
else if (this.$store.state.rooms.joinedRooms.includes(room)) {
this.$store.dispatch(RoomTypes.CURRENT_ROOM, room);
this.$emit('onClose')
}
}
},

Kotlin: lambda run alternative scenario

I have userDto, contains programs, which contains actual field. Actual program can be only one. I need to get it. Than, I run this:
userDto.programs.sortedBy { it.created }.findLast { it.actual }?
Okay, but I want to foresee case, when findLast returns null, & throw exception. Please, advice, how to do it?
UPD:
ProgramType.valueOf(userDto.programs
.sortedBy { it.created }
.findLast { it.actual }
//check here
!!.programType!!).percentage
You are pretty close actually :)! What you could do is:
userDto.programs.sortedBy { it.created }.findLast { it.actual } ?: throw RuntimeException()
Or if you're trying to actually avoid throwing an error(couldn't really tell with the way question is asked), you could just do an error check like this:
userDto.programs.sortedBy { it.created }.findLast { it.actual }?.let{
//rest of your code goes here
}
Hope this helps, cheers!

Objective-C NS_ENUM not recognized in Swift

I am using the google VR SDK external framework in my swift project.
In my Bridging-Header.h I am importing the framework header
#import "GVRCardboardView.h"
In GVRCardboardView.h there is this:
typedef NS_ENUM(NSInteger, GVRUserEvent) {
kGVRUserEventBackButton,
kGVRUserEventTilt,
kGVRUserEventTrigger,
};
In my swift code
#objc func cardboardView(_ cardboardView: GVRCardboardView!, didFire event: GVRUserEvent) {
switch (event) {
case kGVRUserEventBackButton:
print("User pressed back button");
break;
case GVRUserEvent.kGVRUserEventTilt:
print("User performed tilt action");
break;
case .kGVRUserEventTrigger:
print("User performed trigger action");
break;
}
}
But I get compiler errors. You can see I'm trying three different ways to access the enum values in my switch case, and they all give errors. First,
Use of unresolved identifier 'kGVRUserEventTilt'
When I searched other similar questions online, I saw the solution was to include the name of the enum, like you see in the second case of my switch statement. But that gives me this error:
Type 'GVRUserEvent' has no member 'kGVRUserEventTilt'
And the third case gives this:
Pattern cannot match values of type 'GVRUserEvent'
Anyone know why? It recognizes GVRUserEvent as something -- I don't get errors in the argument line.
Thanks for any help!

Assert with string argument not working as expected

EDIT: The issue was with the assert as people pointed out below. Thanks for the help!
I have a enum set that i'm trying equate, but for some reason its not working.
Its declared like so:
typedef NS_ENUM(NSUInteger, ExUnitTypes) {
kuNilWorkUnit,
kuDistanceInMeters,
//end
kuUndefined
};
And i'm using it here:
+(NSString*) ExUnitDescription: (ExUnitTypes) exUnit
{
if (exUnit == kuNilWorkUnit)
{
assert("error with units");
}
///.... more stuff
}
Xcode isnt triggering my assert. EDIT: the assert is just for testing. i've used NSLog as well. The conditional isn't evaluating to true even though the value is clearly kuNilWorkUnit.
Does anyone have any suggestions or ideas of what i'm doing wrong?
You want to do this:
+(NSString*) ExUnitDescription: (ExUnitTypes) exUnit
{
assert(exUnit != kuNilWorkUnit);
///.... more stuff
}
This is because, assert only stops execution if the expression you pass to it is false. Since a string literal is always non-zero, it will never stop execution.
Now, since you are using Objective C and it also looks like you want to have a message associated with your assert, NSAssert would be preferable.
+(NSString*) ExUnitDescription: (ExUnitTypes) exUnit
{
NSAssert(exUnit != kuNilWorkUnit, #"error with units");
///.... more stuff
}

Can IDEA do the same if/else suggestions as Resharper?

IDEA's developers are also developers of Resharper. And I like the way Resharper suggests refactoring if/else.
Can IDEA do the same? this feature does not come by default.
EDIT
Here are some Resharper feature I am referring to:
Original code:
static public string ToNonNullString(this XmlAttribute attr)
{
if (attr != null)
return attr.Value;
else
return string.Empty;
}
Suggestion: remove redundant 'else' resulting in following:
static public string ToNonNullString(this XmlAttribute attr)
{
if (attr != null)
return attr.Value;
return string.Empty;
}
Source is here (
Code suggestions by Resharper making code less readable?).
I remember when I use Visual Studio with Resharper I get a lot of similar suggestions. Have not seen that in IDEA although I've been using it for 2 years.
Yes, it can. Using this Java code:
public class Test {
public String notNull(String str) {
if (str != null) {
return str;
} else {
return "empty";
}
}
}
I get the following suggestions when pressing Alt+Enter (Show intentions) on the else:
Selecting Remove Redundant 'else' converts the method to this:
public String notNull(String str) {
if (str != null) {
return str;
}
return "empty";
}
You can make IDEA report these automatically when you run an inspection. In Settings, go to Inspections, find Confusing 'else' branch in the Control flow-category. And check the Also report when there are no more statements after the 'if' statement.
Then running an inspection with that profile will show that you have a confusing else branch, and you can fix it with a click.