https://kotlinlang.org logo
Docs
Join the conversationJoin Slack
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by Linen
announcements
  • w

    wollnyst

    01/29/2019, 2:20 PM
    Is it possible to convert a
    kotlinx.io.core.ByteReadChannel
    to a
    kotlinx.io.core.BytePacketBuilder
    ? From reading the code the both do similar things.
    d
    • 2
    • 5
  • n

    Nicholas Bilyk

    01/29/2019, 3:45 PM
    Is anybody getting class read errors with kts scripts on Mac JVM 11? WARN: Could not read file: /Users/fuzzyweapon/.jabba/jdk/1.11.0-2/Contents/Home!/modules/java.base/java/io/File.class; size in bytes: 17596; file type: CLASS
    a
    • 2
    • 1
  • h

    halirutan

    01/29/2019, 5:40 PM
    Hey everyone. Are Kotlin DSL for Gradle questions on topic here? I'm using the [gradle-intellij-plugin](https://github.com/JetBrains/gradle-intellij-plugin) and I recently switched from a Groovy
    build.gradle
    to
    build.gradle.kts
    . If I use the official release version of the gradle-intellij-plugin, everything works out and I can simply include it by using
    plugins {
      id("org.jetbrains.intellij") version "0.4.2"
      id("java")
    }
    However, I need to use the latest snapshot version of this for Gradle 5.1.1, so I re-wrote this Groovy code
    buildscript {
      repositories {
        mavenCentral()
        maven {
          url "<https://oss.sonatype.org/content/repositories/snapshots/>"
        }
        maven { 
          url '<http://dl.bintray.com/jetbrains/intellij-plugin-service>' 
        }
        
      }
      dependencies {
        classpath "org.jetbrains.intellij.plugins:gradle-intellij-plugin:0.5.0-SNAPSHOT"
      }
    }
    
    apply plugin: 'org.jetbrains.intellij'
    to
    buildscript {
      repositories {
        mavenCentral()
        maven("<https://oss.sonatype.org/content/repositories/snapshots/>")
        maven("<http://dl.bintray.com/jetbrains/intellij-plugin-service>")
    
      }
      dependencies {
        classpath("org.jetbrains.intellij.plugins:gradle-intellij-plugin:0.5.0-SNAPSHOT")
      }
    }
    
    plugins {
      id("org.jetbrains.intellij") version "0.5.0-SNAPSHOT"
      id("java")
    }
    d
    • 2
    • 4
  • n

    neil

    01/29/2019, 6:18 PM
    My team and I want to stop commenting on formatting issues in Pull Requests. We have added ktlint to the project and want to integrate it into our workflow as painlessly as possible. Does anyone have any tips or experiences on how they integrated ktlint (or any other linter) with their kotlin workflow?
    j
    m
    +5
    • 8
    • 13
  • d

    Davide Giuseppe Farella

    01/29/2019, 10:20 PM
    Declaring
    class MyClass {
        fun CoroutineScope.run() 
    }
    MyClass
    become the context of
    run()
    , while
    CoroutineScope
    become the "owner". That makes sense... But what I want the opposite effect? Sometimes I use this pattern
    class Mapper<E, P> {
        fun <T> invoke( block: Mapper.() -> T ): T
        fun E.toPojo(): P
        fun P.toEntity(): E
    }
    And makes sense that
    Mapper
    is the context for call
    mapper { entity.toPojo() }
    But back to the first example, doesn't make sense to call
    launch {
        with( myObj ) { run() } 
    }
    I would simply call
    launch {
        myObj.run()
    }
    Is that possibile?
    g
    • 2
    • 11
  • j

    Jeff Thomas

    01/29/2019, 10:36 PM
    Hello everyone, I am having a small problem with co-variance. I am using two different Java libraries and one returns a Class<in T> (Class<? super T>) and the other expects a Class<T> as an argument. I am getting a type mismatch "Required: Class<T> Found: Class<in T!>!". I am sure there is a simple solution 🙂 but I can't figure out how to get from the one to the other. Any tips?
    m
    • 2
    • 2
  • d

    Dmytro Danylyk

    01/30/2019, 5:24 AM
    If I share code via https://play.kotlinlang.org is this going to be available permanently or there is some expiration date?
    🤔 3
    l
    j
    • 3
    • 2
  • b

    Bernhard

    01/30/2019, 8:54 AM
    hm, kotlin.parallel.tasks.in.project=true doesn’t look like it’s threadsafe
    g
    • 2
    • 3
  • b

    bodiam

    01/30/2019, 9:15 AM
    Hi all, I was interested in doing some micro benchmarks for Kotlin vs Java, but also Kotlin 1.3.11 vs Kotlin 1.3.20. I found some earlier code based on Kotlin Hidden Costs (https://sites.google.com/a/athaydes.com/renato-athaydes/posts/kotlinshiddencosts-benchmarks). I've updated the code to the latest JMH, and I've ran the code for Kotlin 1.3.11 and 1.3.20. I've found some surprising results, but I might also be misinterpreting the results. Is there anyone here who has some knowledge on JMH/Microbenchmarks, and who can a look at some of my results?
    k
    • 2
    • 52
  • n

    Narek Mailian

    01/30/2019, 2:55 PM
    Heya! Anyone got an example on how to get kotlinx.serialization to work on kotlin native? From what I understand, reflection is not available, but I can't figure out how to parse JSON without using it...
    h
    • 2
    • 1
  • x

    xenoterracide

    01/30/2019, 6:42 PM
    does intellij have an sort of assistance to stub all key/value pairs in a kotlin constructor?
    g
    • 2
    • 1
  • d

    dmcg

    01/30/2019, 8:44 PM
    https://stackoverflow.com/questions/54449230/how-can-i-specify-a-generic-type-parameter-in-a-kotlin-deprecated-annotation
    r
    • 2
    • 1
  • j

    Joe

    01/30/2019, 11:10 PM
    I'm trying to do something here with generics and can't quite get it to work without an unsafe cast. Basically, I'm using jOOQ (http://jooq.org), and want to create a condition in similar ways on different Field<T>s in different places in the code. So the Field in question and the value I want to compare it against change, but the joining of those field/values is relatively the same. here's what I have so far, which works, but I have to cast my Field<*> to a Field<Any> in order to actually use its
    eq()
    method -- i need the same cast if i change it to <out Any>, and if i change it to <in Any> the
    createCondition()
    function works but I can't invoke it in `main()`:
    data class ConditionData<T>(
        val field: Field<T>,
        val value: String,
        val transform: (String) -> T
    )
    
    fun createCondition(vararg things: ConditionData<*>): Condition {
        return things
            .map {
                val value = it.transform.invoke(it.value)
                (it.field as Field<Any>).eq(value)
            }
            .reduce { a, b -> a.and(b) }
    }
    
    fun main() {
        print(
            createCondition(
                ConditionData(DSL.sum(Tables.DATA.WATCH_TIME), "1234567.234") { s -> BigDecimal(s) },
                ConditionData(Tables.DATA.MONTH, "2018-07-01") { s -> LocalDate.parse(s) },
                ConditionData(Tables.DATA.CHANNEL, "HBO") { s -> s }
            )
        )
    }
    output of the main is correct:
    (
      sum("data"."watch_time") < 1234567.234
      and "data"."month" < date '2018-07-01'
      and "data"."channel" < 'HBO'
    )
    g
    • 2
    • 2
  • u

    4ntoine

    01/31/2019, 5:50 AM
    patterns.ini
    file is in resources (jvmTest) but i can't load it -
    getResourceAsStream()
    returns
    null
    i
    • 2
    • 1
  • u

    4ntoine

    01/31/2019, 6:03 AM
    Also tried
    this.javaClass.classLoader.getResourceAsStream("patterns.ini")
    and put
    patterns.ini
    in the package of class in
    resources
    - nothing helps
    d
    s
    • 3
    • 15
  • a

    Alexey Pushkarev

    01/31/2019, 9:42 AM
    Hey Jake @jw , why did you decide to implement reflection based dagger version ? https://github.com/JakeWharton/dagger-reflect. What's purpose?
    g
    g
    +3
    • 6
    • 8
  • g

    GarouDan

    01/31/2019, 1:45 PM
    Why
    Vue.js
    official plugin is not supported anymore in IntelliJ 2018.3.3?
    • 1
    • 2
  • d

    Dalinar

    01/31/2019, 2:16 PM
    is a private setter for a var in the constructor still not possible?
    a
    • 2
    • 2
  • t

    Tsvetozar Bonev

    01/31/2019, 3:33 PM
    Is there a Kotlin implementation (or API) of the Myers Diff Algorithm out there somewhere that is stable enough for use?
    n
    p
    • 3
    • 2
  • b

    Benoît

    01/31/2019, 4:31 PM
    Hey guys! Could I get your opinion on a class I've made in order to add backing fields on extension variable ? Don't pay attention to the bad example I use with the theming stuff, I know there's much better ways to achieve theming in Android. I'm more interested on what you think of this as a solution (Kotlin-wise) in order to have backing fields on classes without having to extend them https://medium.com/@thorebenoit/kotlin-extension-variables-with-backing-field-b430fc6d23e4
    h
    d
    +2
    • 5
    • 19
  • x

    xenoterracide

    01/31/2019, 10:07 PM
    can kotlin jvm be told to write out parameter names? equivalent to the way java 8+ can?
    n
    c
    i
    • 4
    • 5
  • g

    geekGuy

    02/01/2019, 1:16 AM
    I just got here. I'm so new to Kotlin I don't how to start 😦. Can somebody help me with tips or suggestions???
    b
    n
    d
    • 4
    • 4
  • n

    Nikky

    02/01/2019, 3:04 AM
    does anyone know if there is kotlin bindings for libgit2 or a kotlin implemenation of git like JGit ? if not i might just fall back to running git commands directly.. i just plan to do simple things like clone, add, commit and tag, push
    c
    • 2
    • 2
  • m

    muralimohan962

    02/01/2019, 5:37 AM
    interface SomeInterface { fun getName(): String } interface SomeInterface { val name: String } Which is recommended?
    p
    a
    • 3
    • 3
  • e

    edwardwongtl

    02/01/2019, 8:26 AM
    Is there a
    List
    method in stdlib which converts
    [1, 2, 3, 4]
    into
    [[1, 2], [2, 3], [3, 4]]
    ?
    m
    g
    +3
    • 6
    • 9
  • m

    Marc Knaup

    02/01/2019, 10:48 AM
    What is the
    kotlinp
    library? 😄
    d
    • 2
    • 1
  • g

    guppyfaced

    02/01/2019, 11:40 AM
    Any ideas how to create a
    data class
    using parameters? I'm thinking of doing it with a map of values, but if there's a constructor method I could call, that would be great.
    d
    s
    • 3
    • 14
  • b

    Benoît

    02/01/2019, 2:12 PM
    Hello everyone ! Yesterday I posted an implementation for a possible memory/thread safe solution to add backing fields on extension variables. Thanks to your feedback I've improved it, here's the current state. Feel free to let me know if you see anything that could be improved. Cheers guys https://github.com/benoitthore/Enamel/tree/master/EnamelKotlin/src/main/java/com/thorebenoit/enamel/kotlin/core/backingfield
    h
    • 2
    • 3
  • s

    snowe

    02/01/2019, 9:43 PM
    Does anyone have an alternative to the Gradle Lint plugin for the kotlin dsl?
    n
    t
    g
    • 4
    • 5
  • j

    jameskleeh

    02/02/2019, 7:01 AM
    Hey folks, new to Kotlin in general and new to kotlintest. My test class is extending a base class which extends BehaviorSpec. I’d like to execute a method on that base class from within a given block. I’ve tried things like
    super.method()
    , but I keep getting unresolved reference. Any tips are appreciated
    s
    p
    • 3
    • 31
Powered by Linen
Title
j

jameskleeh

02/02/2019, 7:01 AM
Hey folks, new to Kotlin in general and new to kotlintest. My test class is extending a base class which extends BehaviorSpec. I’d like to execute a method on that base class from within a given block. I’ve tried things like
super.method()
, but I keep getting unresolved reference. Any tips are appreciated
s

Shawn

02/02/2019, 7:31 AM
can you share how, exactly, you’re trying to call the super method?
super.method()
should work fine normally
j

jameskleeh

02/02/2019, 7:31 AM
Sure
class BindingSpec: AbstractRabbitMQTest({

    val specName = javaClass.simpleName

    given("A basic producer and consumer") {
This is defined inside
AbstractRabbitMQTest
fun startContext(): ApplicationContext {
...
    }
right inside that given block I’d like to call that method
abstract class AbstractRabbitMQTest(body: AbstractBehaviorSpec.() -> Unit = {}): BehaviorSpec(body) {
s

Shawn

02/02/2019, 7:34 AM
This is an advanced hierarchy you’ve got going here lol
I’m not entirely sure if this is the right answer, but a
Foo.() -> Unit
-typed lambda isn’t actually anything special - it’s essentially a
(Foo) -> Unit
but with
this
in play
Which means that the lambda doesn’t have access to private members or the class’s
super
instance
j

jameskleeh

02/02/2019, 7:37 AM
What do you suggest I try?
s

Shawn

02/02/2019, 7:38 AM
Hmmm, it’s hard to say because I can’t test with your specific setup
let me pull up a scratch file real quick
j

jameskleeh

02/02/2019, 7:43 AM
I appreciate it
s

Shawn

02/02/2019, 7:44 AM
Agh, I wish I had a better answer for you unfortunately. I would maybe ask if you strictly need to have this implementing class extend your abstract class
j

jameskleeh

02/02/2019, 7:45 AM
Unfortunately yes because it contains static state that is shared across all the tests
all the tests that extend from it anyway
s

Shawn

02/02/2019, 7:45 AM
If you need to call the super constructor with a lambda that calls a method that internally calls a super method, you’ve got a bit of an ouroboros on your hands
j

jameskleeh

02/02/2019, 7:46 AM
hahaha
I’m surprised this is difficult
s

Shawn

02/02/2019, 7:46 AM
It might not be, I might just be dumb and not seeing a simple solution here haha
is there maybe a less crazy way to access the state you need? Maybe some top-level vals that are internal/package accessible? Is the parent class abstract? if not, maybe instead have a property that stores a reference to a singleton instance of said parent class or something?
j

jameskleeh

02/02/2019, 7:49 AM
unfortunately I need to create and teardown the object inside the given block
since it is only scoped for that test
s

Shawn

02/02/2019, 7:50 AM
ah I think I get what you mean
j

jameskleeh

02/02/2019, 7:51 AM
I’ve gotta run for now, but I appreciate the attempt!
s

Shawn

02/02/2019, 7:51 AM
Sorry I couldn’t be of more help
I gotta get some shuteye though haha
p

Pavlo Liapota

02/02/2019, 8:39 AM
I have simplified your code this way:
abstract class Parent(
    body: () -> Unit
) {
    fun needToCall() { }
}

class Child : Parent {
    constructor() : super({
        super.needToCall()
    })
}
As you can see error message is
Cannot access '<this>' before superclass constructor has been called
. So it is not possible to do what you want.
Why would you want to call a method on an object that is not initialized yet anyway? Can you make your method static, i.e. put it into companion object?
j

jameskleeh

02/02/2019, 1:28 PM
@Pavlo Liapota I’m not attempting to call the method in the child constructor, but yes I probably could move it to the companion. Thanks!
View count: 3