importre / kotlin-unwrap Goto Github PK
View Code? Open in Web Editor NEW:gift: Unwrap nullable variables in Kotlin
License: Apache License 2.0
:gift: Unwrap nullable variables in Kotlin
License: Apache License 2.0
inline fun <T1, T2, R> unwrap(
t1: T1?,
t2: T2?,
block: (T1, T2) -> R,
block2: () -> R): R? {
return if (t1 != null
&& t2 != null) {
block(t1, t2)
} else {
block2()
}
이런식으로 탔으면 좋겠다 싶을때가 있었는데, 이렇게 만들지 않고 할 수 있는 방법이 있을까요?
In the syntax unwrap(x) {x -> } nah {}
the nah
function naming is not ideal.
I suppose NAH is an acronym for "null a.. h.."? Hm, I have no idea. Anyway, it would read better if it were a natural english word, like unwrap.. or ..
. Additionally, NAH
very strongly resembles a Russian expletive for "go f*** yourself" :(
I think in Func.kt file you can check variables in more elegant way:
val hasNull = arrayOf<Any?>(t1, t2).contains(null)
if (!hasNull) block(t1, t2)
return Unwrap(valid = !hasNull)
Instead of
return Unwrap().apply {
if (t1 != null
&& t2 != null) {
valid = true
block(t1, t2)
}
}
To do it you should make valid
field as constructor parameter. It make your code more readable and maintanable and also allow explicitly specify valid
field that is more preferable.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.