todd.ginsberg
02/17/2022, 1:36 PMval
as the name of a property is annoying and not idiomatic, but I can live with that if they truly can’t fix that. (Note: I fully realize that JB has absolutely nothing to do with leetcode, just asking if there’s been any discussions re: versioning)Ryan Rolnicki
02/19/2022, 2:52 PMVivek Modi
02/19/2022, 6:34 PMdewildte
02/21/2022, 12:58 AMandylamax
02/21/2022, 12:50 PMSola
02/21/2022, 4:39 PMkenkyee
02/22/2022, 3:39 PMTrammel May
02/22/2022, 7:59 PMYoussef Shoaib [MOD]
02/23/2022, 6:11 PMfun main() {
println((2 + 2 == 4) `?` {42} `:` {5})
}
@JvmName("questionMark")
infix fun <T> Boolean.`?`(block: () -> T): IfResult<T> = IfResult<T>(if(this) block() else ElseIndicator)
@Suppress("INVALID_CHARACTERS")
@JvmName("colon")
infix fun <T> IfResult<T>.`:`(block: () -> T): T = if(isElse) block() else result as T
object ElseIndicator
@JvmInline value class IfResult<T> internal constructor(internal val result: Any?) {
val isElse get() = result == ElseIndicator
}
(psssst, it works for bitwise operators too...)bryan
02/24/2022, 3:15 AMtrevjones
02/24/2022, 6:09 PMSam
02/25/2022, 2:24 PMreturn
is an expression (of type Nothing
), you can return return
.
fun madness() {
return return return return return return return return return return
}
vova.bilous
02/28/2022, 5:16 PMursus
03/01/2022, 3:57 AMMarc Reichelt
03/03/2022, 5:02 PMThrowable()
, to only get the file / class name (performance issues)
• or: using reified
to get the class name (will print weird log tags when scope changes (e.g. it will print CoroutineScope
in some instances))bogdoll
03/05/2022, 1:39 PMLeoColman
03/06/2022, 1:14 AMpablisco
03/08/2022, 11:49 PMSam
03/10/2022, 2:02 PM<http://DeprecationLevel.INFO|DeprecationLevel.INFO>
. It would be super useful within a large codebase, where I need to signal intent to other developers without littering the place with compiler warnings. Is it something that's ever been discussed? I'll raise a ticket if not.kqr
03/13/2022, 3:37 PMtherealbluepandabear
03/13/2022, 11:03 PMJason5lee
03/15/2022, 8:26 AMclass SomethingNotFoundException : Exception()
// DB query side
if (result.isEmpty()) {
throw SomethingNotFoundException()
}
// API implement
try {
....
} catch (e: SomethingNotFoundException) {
return HttpResponse(404, "SOMETHING_NOT_FOUND")
}
2.
// DB query side
if (result.isEmpty()) {
throw HttpException(404, "SOME_THING_NOT_FOUND")
}
where HttpException
is handled by the framework or a single catch
that extracts the response parameter from the exception fields.
The second approach is simpler, but it couples with the protocol as you involves the HTTP exception and related concept like status code.
Which one would you choose?Jason5lee
03/15/2022, 10:21 AMsealed class QueryResult {
data class Success(...) : QueryResult()
object NotFound : QueryResult()
object SomeOtherError : QueryResult()
}
// API Code
when(result) {
is QueryResult.Success -> HttpResponse(200, "...")
QueryResult.NotFound -> HttpResponse(404, ...)
...
}
2.
sealed class QueryResult {
data class Success(...) : QueryResult()
data class Error(val status: HttpStatus, ...) : QueryResult()
}
// API Code
when (result) {
is QueryResult.Success -> HttpResponse(200, ...)
is QueryResult.Error -> HttpResponse(result.status, ...)
}
The second way is more convenient but the Error
type is coupled with the HTTP protocol.Olek Brzozowski
03/15/2022, 12:45 PMthrow throw RuntimeException("i am double throw")
Youssef Shoaib [MOD]
03/16/2022, 12:42 AMfun ArmBuilder.multiply2(
first: ArmTransferableValueOperand, second: ArmTransferableValueOperand
): ArmCalculationOperand = buildSubroutine("multiply2", first, second) {
var operand1 by register(first)
var operand2 by register(second)
operand1.compare(operand2, "optimizeSmallerValue") {
greaterThan("swapValues") {
swap(operand1 as ArmStorageOperand, operand2 as ArmStorageOperand)
}
}
result = constant(0)
val mainLoop by labeled
val mainLoopEnd by labeled
label(mainLoop)
operand1.compare(constant(0), "rangeCheck") {
lessThan = mainLoopEnd
equal = lessThan
}
If(operand1 and constant(1), "oddCheck") {
result += operand2
}
operand2 = operand2 shl constant(1)
operand1 = operand1 shr constant(1)
branch(mainLoop)
label(mainLoopEnd)
}
fun ArmBuilder.multiply3(
first: ArmTransferableValueOperand, second: ArmTransferableValueOperand
): ArmCalculationOperand = buildSubroutine("multiply3", first, second) {
var operand1 by register(first)
var operand2 by register(second)
If(operand1 greaterThan operand2, "swapValues") {
swap(operand1 as ArmStorageOperand, operand2 as ArmStorageOperand)
}
result = constant(0)
val mainLoop by labeled
val mainLoopEnd by labeled
label(mainLoop)
If(operand1 lessThanOrEqual constant(0), "rangeCheck") {
branch(mainLoopEnd)
} Else If(operand1 and constant(1), "oddCheck") {
result += operand2
}
operand2 = operand2 shl constant(1)
operand1 = operand1 shr constant(1)
branch(mainLoop)
label(mainLoopEnd)
}
fun ArmBuilder.multiply4(
first: ArmTransferableValueOperand, second: ArmTransferableValueOperand
): ArmCalculationOperand = buildSubroutine("multiply4", first, second) {
var operand1 by register(first)
var operand2 by register(second)
If(operand1 `>` operand2, "swapValues") {
operand1.out `<->` operand2.out
}
result = 0.c
While(operand1 `>` 0.c, "mainLoop") {
If(operand1 `&` 1.c, "oddCheck") {
result += operand2
}
operand2 = operand2 `<<` 1.c
operand1 = operand1 `>>` 1.c
}
}
fun ArmBuilder.multiply5(
first: ArmTransferableValueOperand, second: ArmTransferableValueOperand
): ArmCalculationOperand = buildSubroutine("multiply4", first, second) {
val operand2 = register(second)
result = 0.c
For(first, { it `>` 0.c }, { it `>>=` 1.c; operand2 `<<=` 1.c }, "mainLoop") { operand1 ->
If(operand1 `&` 1.c, "oddCheck") {
result += operand2
}
}
}
(Yes some of these look horrendous, but it's all meant just to show off the current features with a trivial example)Marcello Galhardo
03/16/2022, 9:33 AMYoussef Shoaib [MOD]
03/17/2022, 11:28 AMRob Elliot
03/17/2022, 3:56 PMModuleLayer
and how to use it.Jason5lee
03/19/2022, 2:39 AMursus
03/19/2022, 11:01 PM