jaqxues
05/19/2020, 2:02 PMclass Preference<T : Any> private constructor(
val key: String,
val default: T,
val type: KClass<T>
)
// Extension Function
fun getPref(): T { // This should be a nullable generic set by the preference
// parse and return pref
}
Now, not every preference is not-null. So how can I omit <T: Any>
to get to <T>
so the preference itself can set itself nullable only via its type generic?
The problem is that KClass does not like <T> and requires <T: Any>
is there any smart way to get a not-null type generic from a nullable one? Or a reified function way to handle that?Orhan Tozan
05/19/2020, 4:17 PMfun <T, R> Result<T>.letIfSuccess(
block: (data: T) -> Result<R>
): Result<R> = when (this) {
is Result.Failure -> this
is Result.Success -> block(this.data)
}
letIfSuccess returns Result<R>
, but returns Result<T>
when first branch of when statement is met.
I would expect it to give it a compile error, as Result<T> !is Result<R>
.elect
05/20/2020, 9:11 AMclass Function1<A : Number, R : Number>(val func: (a: A) -> R) {
operator fun invoke(a: A): R = func(a)
}
why ::invoke
seems to require a parameter of type Nothing
here:
when(val f = t.function) {
is Function1<*, *> -> f.invoke(stack.pop()) // Type mismatch: required Nothing
}
Because of the <*, *>
? I cant specify anything else because of type erasure
this works though
is Function1<*, *> -> (f as Function1<Number, Number>)(stack.pop())
Nikhil
05/20/2020, 10:09 AMJavier
05/20/2020, 10:25 AMJoan Colmenero
05/20/2020, 11:28 AMclass MyResponse : ArrayList<MyResponseItem>
How do I create an object of MyResponse with a list of MyResponseItem?elye
05/20/2020, 12:19 PMbmarinovic
05/20/2020, 1:42 PMwhen
block (hasMaxRole
is extension method on my User
data class):
if (loggedInUser.hasMaxRole(<http://Role.NO|Role.NO>_ROLE)) {
ResponseEntity(HttpStatus.FORBIDDEN)
} else if (loggedInUser.hasMaxRole(Role.USER_ROLE)) {
if (loggedInUser.id == id) {
ResponseEntity.ok(loggedInUser)
} else {
ResponseEntity(HttpStatus.FORBIDDEN)
}
} else {
userApiService.findById(id)
}
I would like something like this:
when(loggedInUser) {
hasMaxRole(<http://Role.NO|Role.NO>_ROLE) -> ResponseEntity(HttpStatus.FORBIDDEN)
hasMaxRole(Role.USER_ROLE) && (loggedInUser.id == id) -> ResponseEntity.ok(loggedInUser as Any)
hasMaxRole(Role.USER_ROLE) -> ResponseEntity(HttpStatus.FORBIDDEN)
else -> userApiService.findById(id)
}
(even with this code, I am repeating loggedInUser
in second match since I need to call it twice in && operation)pp.amorim
05/20/2020, 1:56 PMval intValue: Int = 2498533
val foo = "<http://abc.com/$intValue|abc.com/$intValue>"
println("Foo: $foo") //Foo: <http://abc.com/٢٤٩٨٥٣٣|abc.com/٢٤٩٨٥٣٣>
But note that the interpolation is returning these non integer characters. Why this is happening? Shouldn’t it be <http://abc.com/2498533|abc.com/2498533>
?Ellen Spertus
05/20/2020, 10:22 PMList<String>
and a function f: String -> T?
and would like to apply f
to each element s
of the list in order until f(s)
is non-null, returning that f(s)
. If f(s)
is null for all values, null
should be returned. Here’s an ugly implementation:
fun g(list: List<String>): String? =
if (list.isEmpty()) {
null
} else {
f(list.first()) ?: g(list.subList(1, list.size))
}
Can someone suggest a more elegant way?
Here’s the code, if you’d like to play with it. https://pl.kotl.in/mByy6tkphIfvwm
05/21/2020, 5:49 AMIfvwm
05/21/2020, 6:12 AMUziel Sulkies
05/21/2020, 6:25 AMdata class Thing (
val prop: String
) {
private fun getProp(): Int = 5
}
Although this code won’t compile:
data class Thing (
val prop: String
) {
private fun getProp(): String = "a"
}
with error “Platform declaration clash: The following declarations have the same JVM signature (getProp()Ljava/lang/String;)”
The first one compiles successfully, and the getProp
function can be either private
or public
.
In Java, the same code won’t compile:
public class Test {
private String prop;
public final String getProp() {
return prop;
}
public void setProp(String prop) {
this.prop = prop;
}
private final int getProp(){
return 5;
}
}
With error: “‘getProp()’ is already defined in ‘com.ni.monday.model.domain.webhooks.events.Test’”
So why is it allowed in Kotlin?
The consequences of this is this bug, I opened yesterday on #jackson-kotlin : https://github.com/FasterXML/jackson-module-kotlin/issues/341James Richardson
05/21/2020, 8:33 AMelect
05/21/2020, 1:01 PMtailrect
function?
var result = 1
for (i in 1..a)
result *= i
result
Sam Garfinkel
05/21/2020, 6:26 PMk
then Map[k]
is non-null? Or do I need to write my own?Leon johnson
05/21/2020, 8:02 PMLeon johnson
05/21/2020, 11:56 PMursus
05/22/2020, 2:58 AMpromoCodeGenerationCountExceeded
is false when it should be truePatrick
05/22/2020, 11:07 AMShreyas Patil
05/22/2020, 6:38 PMsteenooo
05/22/2020, 9:44 PMKBrewster
05/23/2020, 2:20 PMSamkeene
05/23/2020, 3:34 PM@ExperimentalTime
, is there a way to disable this requirement?Sam Garfinkel
05/23/2020, 5:27 PMKroppeb
05/24/2020, 5:23 PMzero_coding
05/24/2020, 6:39 PMpambrose
05/24/2020, 7:36 PMmyaa
05/24/2020, 10:54 PMIfvwm
05/25/2020, 1:35 AMIfvwm
05/25/2020, 1:35 AMNikky
05/25/2020, 2:08 AMthis
from a class outside of current scope which may have a different this
Shawn
05/25/2020, 2:11 AM