Skip to content

Instantly share code, notes, and snippets.

@romyilano
Last active November 11, 2025 22:13
Show Gist options
  • Select an option

  • Save romyilano/a73e8fc9a25fac1856abce9035ff4a48 to your computer and use it in GitHub Desktop.

Select an option

Save romyilano/a73e8fc9a25fac1856abce9035ff4a48 to your computer and use it in GitHub Desktop.
kotlin recipe - type check

#kotlin #artifact #programming #codesample

if (obj is String) {
	print(obj.length)
}
if (obj !is String) {
	println("Not a string)
} else {
	println(obj.length)
}

Control flow docu

when (x) {
	is Int -> print(x + 1)
	is String -> print(x.length + 1)
	is IntArray -> print(x.sum())
}

Conditionals in Kotlins

val energy = 90

val passion = 99

val resilience = 80

    val innovationStatus = when(energy) {
        100 -> "High energy but safe harbors don't make great sailors"
        in 90..99 -> "still too perfect conditions to determine if someone is truly good"
        in 75..90 -> { if (resilience < 20) {
                "easy waters easily burn this sailor"
            } else {
                "has some minor wounds"
            }
        }
        in 15..74 -> { 
            if (passion > 50 && resilience > 75) {
            	"this sailor can succeed in all conditions"
        	} else {
                "easy conditions throw this one off"
            }
        }
        else -> { 
            if (resilience > 70 && passion > 70) {
            	"This is a true sailor"
        	} else {
                print("we all have bad luck")
            }
        }   
    }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment