Ran
04/05/2020, 9:27 AMMax Tan
04/05/2020, 9:37 AMStian N
04/05/2020, 11:07 AMPhilipp Mayer
04/05/2020, 12:44 PMclass EmployeeModel(val id: Long, val firstName: String, val lastName: String)
class Employee(val id: Long, val firstName: String, val lastName: String)
is there a more idiomatic way of creating one object from the other than:
EmployeeModel(employee.id, employee.firstName, employee.lastName)
Thank you!jeggy
04/05/2020, 12:48 PMException in thread "main" java.security.AccessControlException: Access control exception due to security reasons in web playground:
access denied ("java.util.PropertyPermission" "kotlin.ignore.old.metadata" "read")
at executors.JavaRunnerExecutor.main (JavaRunnerExecutor.kt:-1)
viralshah
04/05/2020, 3:05 PMprivate val myRowMapper = BiFunction<Row, RowMetadata, Person> { row, _ ->
Person(
row["Id", Integer::class.java]!!.toInt(), row["NAME", String::class.java]!!,
row["StudentId", Integer::class.java]!!.toInt(),
row["GroupId", Integer::class.java]?.toInt(), row["startDate", Instant::class.java]!!,
row["endDate", Instant::class.java], row["authId", Integer::class.java]!!.toInt(),
row["statusId", Integer::class.java]?.toInt(),
row["IS_TEST", Boolean::class.java]!!,
row["DELETED", Boolean::class.java]
)
}
databaseClient.execute(myQuery).bind("Id" to "12345").map(myRowMapper).flow().toList()
data class Person(
val id: Int,
val name: String,
val studentId: Int,
val groupId: Int?,
startDate: Instant,
endDate: Instant?,
authId: Int,
statusId: Int,
isTest: Boolean,
deleted: Boolean?)
Am I doing this correctly. It seems like I have to do an awful lot of conversions just to get a simple data type like Int / BooleanCodic
04/05/2020, 4:06 PMimport com.file2
fun main() { file2.test() }
Alexander Suraphel
04/05/2020, 10:10 PM8938.3f.toString()
pdegand
04/06/2020, 8:25 AMclass UserId { String userId; }
), this is a perfect usecase for Inline classes !
In the documentation, it’s noted that primitive types are usually heavily optimized by the runtime, while their wrappers don't get any special treatment.
Do you have any concrete examples of the kind of optimizations you lose when using wrapper class instead of the primitive type (or the Inline class in KT) ?Joshua
04/06/2020, 8:57 AMarekolek
04/06/2020, 10:33 AMenum class Foo {
ONE, TWO
}
fun fooArray(foos: Array<Foo>) {
println(foos.toList())
}
fun fooVararg(vararg foos: Foo) {
fooArray(foos) // Type mismatch: inferred type is Array<out Foo> but Array<Foo> was expected
}
in case of final classes (including enums), it's weird that vararg
should make the Array
have out
variance
I'm wondering if there's any sense in filing a youtrack issueageery
04/06/2020, 5:35 PMclass Transformer<T, D>(private val seq: Sequence<T>, private val lambda: (T) -> D) {
fun process(): Sequence<D> = seq.mapNotNull { lambda(it) }
}
An example usage would be:
val mySeq = Transformer(sequenceOf("abc"), String::toUpperCase)
However, what if we change the API of the Transformer class to allow the incoming sequence to contain null values but, since these are already filtered out in the process
function before we call the lambda, we want the lambda parameter to only have to deal with not null T values?
Concretely, I'd like this to work:
val mySeq = Transformer(sequenceOf("abc", null), String::toUpperCase)
This doesn't compile because T is a nullable String.
Is there any way that two generic parameters can be tied like that or some way of specifying that the parameter of the lambda is not null?viralshah
04/06/2020, 6:37 PMRodrigo Silva
04/07/2020, 1:52 AMSuspension functions can be called only within coroutine body
Michael de Kaste
04/07/2020, 9:28 AMinterface Code{
private val code: String
}
interface Keyable{
private val key: String
}
fun <T> getThing(obj: T) : Pair<String, String?> where T : Code, T maybe Keyable{
return obj.code to obj.key //implied that since we're not sure that Keyable is implemented, the field becomes nullable
}
iex
04/07/2020, 11:28 AMjava.text.SimpleDateFormat
fail if the date string doesn't match exactly? Tried setting isLenient
to false but it didn't work. Specifically I want that if the format is "yyyy-MM-dd", e.g. "2019-08-05T235959+0200" is NOT parsed. it should only parse if the date string is "2019-08-05"
val str = "2019-08-05T23:59:59+0200"
val format = SimpleDateFormat("yyyy-MM-dd")
format.isLenient = false
format.parse(str) // should return null or crash
hallvard
04/07/2020, 12:00 PMauthor = someNullableFunction() ?: author
, will the compiler be smart enough to turn the statement into a null check, thus avoiding re-setting a value that is not really changing in situations where the function returns null?Chris Cunningham
04/07/2020, 12:27 PMmy_function(*myType)
, and even know kotlin has a spread operator as well it seems to only work on collections and can't be used for stuff like Pair<A, B>jaqxues
04/07/2020, 1:15 PMjava.lang.Executable
. So this small code here in Kotlin
// val resolvedClass: Class<*>
// val resolved = mutableMapOf<MemberDec, Member>()
// MemberDec is a sealed class with MethodDec and ConstructorDec as subclasses containing name and parametertypes
resolved[member] = when (member) {
is MethodDec -> member.findMethod(resolvedClass) // returns Method
is ConstructorDec -> member.findConstructor(resolvedClass) // returns Constructor<*>
}
This fails because of Kotlin smart casts using a common superclass java.lang.Executable
that does not exist on all devices.
java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/reflect/Executable;
at com.jaqxues.akrolyb.genhook.FeatureHelper$Companion.resolveMembers(FeatureHelper.kt:164)
I don't know if this is worth a BugReport on BugTracker, just wanted to let you know.
The code is shared on Github: https://github.com/jaqxues/Akrolyb/blob/316b4b761c6e9b664141b68b951152b1bd805c89/akrolyb/src/main/java/com/jaqxues/akrolyb/genhook/FeatureHelper.kt#L163
Workaround:
// fixme Explicit "Useless" Cast to Member. Kotlin will otherwise cast to Executable, which is only available in Oreo+
@Suppress("USELESS_CAST")
resolved[member] = when (member) {
is MethodDec -> member.findMethod(resolvedClass) as Member
is ConstructorDec -> member.findConstructor(resolvedClass) as Member
}
aaverin
04/07/2020, 1:39 PMwhen
statements?
when(myObject) {
myCustomExtensionFunctionOnMyObject -> do something
}
Edoardo Luppi
04/07/2020, 2:24 PMEllen Spertus
04/07/2020, 9:33 PMType mismatch
error in this code?
private fun applyIfPossible(contacts: List<ContactEntity>): Unit {
contacts.firstOrNull() ?.run { contact ->
println(contact)
}
}
xii
04/07/2020, 9:37 PMxii
04/07/2020, 9:40 PMikozlowski
04/08/2020, 12:07 AMresult.andExpect(status().isOk)
.andReturn()
.response!! // Problematic line
.contentAsClass<SomeType>()
inline fun <reified T> MockHttpServletResponse.contentAsClass(): T? =
TestUtil.convertJsonBytesToObject(this.contentAsString, T::class.java) // This returns T
with !!
Unnecessary non-null assertion (!!) on a non-null receiver of type MockHttpServletResponse!
without !!
Call of inline function with nullable extension receiver can provoke NPE in Kotlin 1.2+
method org.springframework.test.web.servlet.MvcResult.getResponse()
has this docs/signature
/**
* Return the resulting response.
* @return the response, never {@code null}
*/
MockHttpServletResponse getResponse();
I'm using current Kotlin & plugin - 1.3.71
any ideas or should I open issue?Nick
04/08/2020, 3:12 AMe.lislebo
04/08/2020, 6:25 AMiex
04/08/2020, 5:23 PMsealed class VoidOperationState {
object Progress: VoidOperationState()
object Success: VoidOperationState()
data class Failure(val t: Throwable): VoidOperationState()
}
sealed class OperationState<out T> {
object Progress: OperationState<Nothing>()
data class Success<out T>(val data: T): OperationState<T>()
data class Failure(val t: Throwable): OperationState<Nothing>()
}
(Of course I could make VoidOperationState
just an OperationState<Unit>
but that's not nice, since I'd have to be always passing the Unit
to Success
...Sam Garfinkel
04/08/2020, 7:53 PMthis
be passed to an interface delegate when initializing the delegate?
data class Foo(
val name: String
): FooExt by FooExtImpl(this)
interface FooExt {
fun nameExt(): String
}
class FooExtImpl(private val outer: Foo): FooExt {
override fun nameExt() = "Hello, ${outer.name}"
}
I basically need this exact functionality, I’m not able to directly add nameExt
to the data class. If delegates could be initialized inside an init { }
block then that would work but I’m unsure of any actual workarounds for this.Ellen Spertus
04/08/2020, 9:28 PMval r: Runnable = Runnable {
println("In runnable")
r.run()
}
(I know the code would generate a stack overflow; it’s an oversimplification of what I really want to do.)
How can I do this? Changing r
from val
to var
has no effect. Do I need to create a second variable and make it refer to the first value?
lateinit var r2: Runnable
var r: Runnable = Runnable {
println("In runnable")
r2.run()
}
init {
r2 = r
}