Joel Pedraza
07/10/2020, 12:46 AMinline
function from kotlin code without inlining the body at the call site? (similar to how it would be called from java code invoking it from ex: FooKt.myInlinedFun()
). Please spare the the 20 questions for determining if this an XY problem, just a curiosity if such a calling convention exists :)E.Kisaragi
07/10/2020, 3:10 AMdimsuz
07/10/2020, 9:35 AMinterface Validator<T> {
fun validate(input: T)
fun and(other: Validator<T>): Validator<T> = TODO()
}
fun <T> andExternal(v1: Validator<T>, v2: Validator<T>): Validator<T> = TODO()
fun <T> isNotEmpty(): Validator<T> = TODO()
fun <T> isNotBlank(): Validator<T> = TODO()
fun doStuff(validator: Validator<String>) { }
fun main() {
// isNotEmpty is highlighted: "not enough information to infer type variable T"
doStuff(isNotEmpty().and(isNotBlank()))
// no error when using external variant of "and", inferred as String!
doStuff(andExternal(isNotEmpty(), isNotBlank()))
}
bbaldino
07/10/2020, 5:12 PMClass<SomeEnumType>
from a KType
created elsewhere via typeOf<SomeEnumType>()
? I feel like it should be possible, but I'm struggling to find the right call to get it.kagomez
07/10/2020, 11:36 PMrahulsom
07/10/2020, 11:57 PMLastExceed
07/11/2020, 6:34 AMmyMutableInt = myMutableInt + 2
can be simplified to
myMutableInt += 2
but is there any way to do something similar with non-operator functions, e.g. simplifying
myMutableInt = myMutableInt.coerceIn(10, 100)
to something like
myMutableInt .= coerceIn(10,100) //not valid syntax
?Gilles Barbier
07/11/2020, 9:29 AMinline fun <reified T> proxy() = (something...) as T
I can use use as val proxy = proxy<MyClass>()
- but how can I use it from an interface to obtain a proxy of a class implementing this interface?Philipp Mayer
07/11/2020, 10:42 AMMqttMessage
as an operation right on the array?
fun createMessage(message: String): MqttMessage {
val array = message.map { it.toByte() }
.toByteArray()
return MqttMessage(array)
}
Something like:
fun createMessage(message: String): MqttMessage = message.map { it.toByte() }
.toByteArray()
.map { MqttMessage(it) }
But the last map would have to be on the whole array.
Thanks!Slackbot
07/12/2020, 2:22 PMHullaballoonatic
07/12/2020, 6:56 PMNumber
so limited? why doesn't it look something like this, at the very least?
interface Number<N: Number<N> : Comparable<N> {
operator fun plus(other: N): N
operator fun minus(other: N): N
operator fun times(other: N): N
operator fun div(other: N): N
operator fun unaryMinus(): N
val minValue: N
val maxValue: N
fun inverse(): N
fun toDouble(): Double
fun toFloat(): Float
fun toLong(): Long
fun toInt(): Int
fun toChar(): Char
fun toShort(): Short
fun toByte(): Byte
}
am i wrong in thinking all subclasses of Number implement those operators anyways?Ray
07/13/2020, 4:34 AMspring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
Some posts were saying vaguely the problem is status keyword. what’s the issue?JP
07/13/2020, 12:39 PMtry-with-resources
construct in Java to Kotlin, regarding handling exceptions?
// Java
DataSource ds = createDataSource();
try (Connection connection = ds.getConnection()) {
// ...
// just throw exception for simulation
throw new IllegalStateException()
// ...
} catch (SQLException e) {
e.printStackTrace()
}
I’ve tried to use the use
function like this:
// Kotlin
val ds: DataSource = createDataSource()
try {
dataSource.connection.use { connection ->
// just throw exception for simulation
throw IllegalStateException()
}
} catch (e: SQLException) {
e.printStackTrace()
}
But SQLException
is not caught, instead, only IllegalStateException
could be caught.
How could one catch a SQLException
thrown from java.sql.Connection.getConnection()
gracefully using Kotlin, while, if possible, utilizing the use
function to close the connection?Alex Ch
07/13/2020, 1:07 PMNir
07/13/2020, 9:11 PMwith
. It seems like with
has no real advantage, over the extension function form of run
.
Usually flowcharts/blog posts say "use run if you need ?.but even if you don't need
?., you can just always use
.run` instead of with
. It seems like it would be preferable if with
did not exist, so that there would just be fewer scope functions to know with no disadvantage. Am I missing something about with
?Gyuhyeon
07/14/2020, 3:43 AMis
prefixes from boolean when serializing into response body json like val isValid = true => {"isValid": true}
while Spring latest version requires that is
prefix is not included when deserializing from request parameters like ?valid=true => val isValid = true
yet ?ssValid=true
fails to map?
So I either have to mark every boolean in a 20k commit history enterprise project which is linked with a Rest controller like @JsonProperty("valid")
or change the structure so that when consuming my REST api they have to live with "isFoo" boolean naming while when they send back the request to me they have to change the name like "foo" instead in their request bodies.
Absolute bonkers
I'm beginning to think maybe using is
prefix in kotlin is simply a horrible idea that I should always stay away from. It causes way too many compatibility issues and headaches due to the compiled java source treating it as foo
with isFoo()
getter.Gunslingor
07/14/2020, 4:56 AMPaulius Ruminas
07/14/2020, 7:34 AMShreyas Patil
07/14/2020, 2:20 PMinternal
which is hiding that class in Kotlin code. But it's publically visible in Java. How can I avoid that?dimsuz
07/14/2020, 2:28 PM-Werror
and then found a few of those unused in a codebase, no errors. Only IDE gives a warning on unused functions/classes. Can I somehow ask compiler to do this too?Gopal S Akshintala
07/14/2020, 4:07 PMtailrec fun TreeNode.findMin(): TreeNode = left?.findMin() ?: this
But this is: tailrec fun TreeNode.findMin(): TreeNode = if (left == null) this else left!!.findMin()
I am missing the concise syntax, why can’t the compiler smartly make the first one tailrec
andylamax
07/14/2020, 8:12 PMJoel
07/14/2020, 8:49 PMBigDecimal
extensions in the kotlin
package:
operator fun BigDecimal.plus(other: BigDecimal): BigDecimal = add(other, MathContext.DECIMAL128)
operator fun BigDecimal.minus(other: BigDecimal): BigDecimal = subtract(other, MathContext.DECIMAL128)
operator fun BigDecimal.div(other: BigDecimal): BigDecimal = divide(other, MathContext.DECIMAL128)
operator fun BigDecimal.times(other: BigDecimal): BigDecimal = multiply(other, MathContext.DECIMAL128)
The default implementation for div
is:
@kotlin.internal.InlineOnly
public inline operator fun BigDecimal.div(other: BigDecimal): BigDecimal = this.divide(other, RoundingMode.HALF_EVEN)
This will allow my implementation to always use DECIMAL128
standard math context for all operations, eliminating any precision issues (for example 1.toBigDecimal() / 3.toBigDecimal()
results in 1
due to rounding. I want to help my engineers avoid this.
What is the cleanest way to accomplish this? I tried adding a new file in package kotlin
which works but feels like a code smell.Hullaballoonatic
07/14/2020, 11:14 PMval last3 = myList[^3..]
val first3 = myList[..3]
when (numA) {
< numB -> foo()
== numB -> bar()
> numB -> baz()
}
val defaultInt = default(int)
to name a few.
one of the distinct differences between the languages, though, is that c# does not have the type-erasure that the jvm does, so perhaps that allows for default
Joel Pedraza
07/15/2020, 12:30 AMlaimiux
07/15/2020, 1:58 AMwhere
clause when defining typealiases
?Wesley Acheson
07/15/2020, 9:53 AMoverride fun load(id: String): Mono<StoredRequest?> {
val storedRequest: StoredRequest? = memory[id]
return Mono.just(null);
}
However it doesn't allow me to return a Mono of null here or even return the value from the backing map.
As far as I understand the method signature I've given a null should be allowed.
Is there any way to force the java like behaviour?Nir
07/15/2020, 2:21 PMfor (x in foo::class.memberProperties)
, but this only allows you to iterate over properties, i.e. no set function is availableGunslingor
07/15/2020, 3:01 PMFile file = new File(
getClass().getClassLoader().getResource("database.properties").getFile()
);
Is this really the best way to access files in the resources folder?stephanmg
07/15/2020, 3:06 PMstephanmg
07/15/2020, 3:06 PMGunslingor
07/15/2020, 3:16 PMstephanmg
07/15/2020, 3:18 PMGunslingor
07/15/2020, 3:19 PMclientDocker.buildImageCmd().pathToDockerfile()
pathstephanmg
07/15/2020, 3:20 PMval file = MyClass::class.java.getResource("database.properties").getFile()
Gunslingor
07/15/2020, 3:21 PMstephanmg
07/15/2020, 3:22 PMthis.javaClass.getResource("database.properties").getFile()
Gunslingor
07/15/2020, 3:24 PMval clientDocker = DockerClass().dockerClient
val result = clientDocker.infoCmd().exec()
val file = DockerClass::class.java.getResource("database.properties").file
This works, now I just need the path of it.stephanmg
07/15/2020, 3:26 PMGunslingor
07/15/2020, 3:26 PMstephanmg
07/15/2020, 3:29 PMGunslingor
07/15/2020, 3:29 PMstephanmg
07/15/2020, 3:30 PMGunslingor
07/15/2020, 3:32 PMstephanmg
07/15/2020, 3:37 PMGunslingor
07/15/2020, 3:37 PMstephanmg
07/15/2020, 3:38 PMGunslingor
07/15/2020, 3:38 PMakatkov
07/15/2020, 5:02 PMGunslingor
07/15/2020, 6:43 PMfun main(){
val clientDocker = DockerClass().dockerClient
val result = clientDocker.infoCmd().exec()
DockerClass().main()
//clientDocker.buildImageCmd().pathToDockerfile("?????")
}
akatkov
07/15/2020, 6:49 PMGunslingor
07/15/2020, 6:49 PMpackage com.newsworthy.build.containers
import com.github.dockerjava.core.DefaultDockerClientConfig
import com.github.dockerjava.core.DockerClientConfig
import com.github.dockerjava.core.DockerClientImpl
import com.github.dockerjava.httpclient5.ApacheDockerHttpClient
import com.github.dockerjava.transport.DockerHttpClient
import java.io.File
const val projectName: String = "srcBuild"
class DockerClass {
private val config: DockerClientConfig = DefaultDockerClientConfig.createDefaultConfigBuilder()
.withDockerHost("<tcp://localhost:2375>")
//.withDockerTlsVerify(true)
//.withDockerCertPath("/home/user/.docker")
//.withRegistryUsername("admin")
//.withRegistryPassword("password")
//.withRegistryEmail("<mailto:gunslingor@gmail.com|gunslingor@gmail.com>")
//.withRegistryUrl("<https://index.docker.io/v1/>")
.build()
val httpClient: DockerHttpClient = ApacheDockerHttpClient.Builder()
.dockerHost(config.dockerHost)
.sslConfig(config.sslConfig)
.build()
val dockerClient = DockerClientImpl.getInstance(config, httpClient)!!
fun main(){
//val filePath = DockerClass::class.java.getResource("svrDB").file
//val filePath = (this::class).java.getResource("dockerfiles\\svrDb")
//print(filePath)
}
}
fun main(){
val clientDocker = DockerClass().dockerClient
val result = clientDocker.infoCmd().exec()
DockerClass().main()
//clientDocker.buildImageCmd().pathToDockerfile()
}
akatkov
07/15/2020, 6:50 PMsvrDb
file to main/resources instead of test/resources?Gunslingor
07/15/2020, 6:51 PMakatkov
07/15/2020, 6:52 PMsourceSets
in gradle to make it work, but wouldn’t recommend necessarilyGunslingor
07/15/2020, 6:54 PMDockerClass::class.java.getResource("dockerfile/svrDB")
still comes back nullakatkov
07/15/2020, 6:55 PMdockerfiles
in your screenshotGunslingor
07/15/2020, 6:56 PMakatkov
07/15/2020, 6:57 PMInputStream
to it using getResourceAsStream
Gunslingor
07/15/2020, 6:59 PMakatkov
07/15/2020, 6:59 PMDockerClass::class.java.classLoader.getResource("dockerfiles/svrDB")
Gunslingor
07/15/2020, 7:00 PMakatkov
07/15/2020, 7:00 PMGunslingor
07/15/2020, 7:01 PMakatkov
07/15/2020, 7:04 PMgetResourceAsStream
, write that to a File (maybe using File.createTempFile
), then you can pass that path to your function and it’ll work in both dev IDE/prod jar scenariosGunslingor
07/15/2020, 7:06 PMakatkov
07/15/2020, 7:07 PMGunslingor
07/15/2020, 7:24 PMstatic("/static") {
files("build/processedResources/jvm/main")
}
akatkov
07/15/2020, 7:25 PMGunslingor
07/15/2020, 7:27 PM