https://kotlinlang.org logo
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
benchmarks
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
confetti
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
lincheck
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
Title
k

karelpeeters

09/26/2018, 10:39 AM
I need state for both parent classes.
e

Eugenio

09/26/2018, 1:06 PM
like this?
interface Parent1 {
    var state1: String

    fun manipulateState1() {
        state1 += "!"
    }
}

interface Parent2 {
    var state2: Int

    fun manipulateState2() {
        state2 += 1
    }
}

class Child : Parent1, Parent2 {
    override var state1 = "hello"
    override var state2 = 0

    fun manipulateState() {
        manipulateState1()
        manipulateState2()
    }
}
k

karelpeeters

09/26/2018, 1:09 PM
Problem is that that allows
Child
to access both states, there's no encapsulation.
Good idea though
e

Eugenio

09/26/2018, 1:19 PM
oh in that case:
interface Parent1_ {
    var state1: String

    fun manipulateState1() {
        state1 += "!"
    }
}

interface Parent2_ {
    var state2: Int

    fun manipulateState2() {
        state2 += 1
    }
}

interface Parent1 : Parent1_ {
    @Deprecated("", level = HIDDEN)
    override var state1: String
}

interface Parent2 : Parent2_ {
    @Deprecated("", level = HIDDEN)
    override var state2: Int
}

class Child : Parent1, Parent2 {
    override var state1 = "hello"
    override var state2 = 0

    fun manipulateState() {
        manipulateState1()
        manipulateState2()
    }
}
k

karelpeeters

09/26/2018, 1:20 PM
Can
Child
not interact with
state1
and
state2
then? That's evil 😒imple_smile:
e

Eugenio

09/26/2018, 1:28 PM
you'd have to do:
(this as Parent1_).state1
otherwise it's as if it doesn't exist, not even in the autocomplete
or, even better:
interface Parent1 {
    fun manipulateState1()
}

interface Parent2 {
    fun manipulateState2()
}

interface Parent1State : Parent1 {
    var state1: String

    override fun manipulateState1() {
        state1 += "!"
    }
}

interface Parent2State : Parent2 {
    var state2: Int

    override fun manipulateState2() {
        state2 += 1
    }
}

class Child : 
    Parent1 by object : Parent1State { override var state1 = "" },
    Parent2 by object : Parent2State { override var state2 = 0 } 
{
    fun manipulateState() {
        manipulateState1()
        manipulateState2()
    }
}
k

karelpeeters

09/26/2018, 1:29 PM
Might even be able to create classes for those delegate objects then!
e

Eugenio

09/26/2018, 1:29 PM
sure, depends on your case
k

karelpeeters

09/26/2018, 1:29 PM
I'll play around a bit later today, thanks for the ideas!
e

Eugenio

09/26/2018, 1:31 PM
or just this:
fun Parent1(state1: String): Parent1 = object : Parent1State { override var state1 = state1 }
fun Parent2(state2: Int): Parent2 = object : Parent2State { override var state2 = state2 }

class Child :
    Parent1 by Parent1(""),
    Parent2 by Parent2(0)
{
    fun manipulateState() {
        manipulateState1()
        manipulateState2()
    }
}
you're welcome!
k

karelpeeters

09/26/2018, 1:31 PM
Yeah that's what I mean with my last comment.
Don't even need the deprecation then I guess.
e

Eugenio

09/26/2018, 2:54 PM
exactly
k

karelpeeters

09/29/2018, 1:56 PM
I'm kind of stuck again, do you happen to have another trick for
protected
visibility?
interface User {
    fun foo()
}

class ConstUser(size: Int) : User {
    private val values = Array(size) { "$it" }

    override fun foo() = print(values)

    fun get(index: Int) = values[index]
}

class MyUser: User by ConstUser(2) {
    //I want ConstUser.get(index) to be protected-level visible here
}
e

Eugenio

09/29/2018, 2:25 PM
@karelpeeters in this case you might do:
interface User {
    fun foo()
}

class ConstUser(size: Int) : User {
    private val values = Array(size) { "$it" }

    override fun foo() = print(values)

    operator fun get(index: Int) = values[index]
}

open class MyUser 
private constructor (
    private val user: ConstUser
): User by user {
    
    constructor() : this(ConstUser(2))
    
    protected operator fun get(index: Int) = user[index]
}
k

karelpeeters

09/29/2018, 2:30 PM
The annoying thing is that that requires a bunch of code duplication (there are lots of
MyUser
-type classes). I wonder if there's a way to get access to the delegate object instance, that might simplify things a bit.
e

Eugenio

09/29/2018, 2:39 PM
There isn't
You could have an internal method on the interface in another module
k

karelpeeters

09/29/2018, 2:49 PM
Hmm. Now I'm thinking generating code might be a better solution, but that then requires multiple modules and gradle fuckery. Dammit!
e

Eugenio

09/29/2018, 2:55 PM
It won't get easier than this I'm afraid... What's the use case?
k

karelpeeters

09/29/2018, 3:35 PM
Faking multiple inheritance still. I spend some time cleaning up, if you want a full explanation: I'm writing an optimizing compiler inspired by LLVM. It works on an SSA form IR made of `Instruction`s, and those instructions have operands of type
Value
. Instructions themselves are also values, representing the result of their computation (this works and is elegant because of SSA, don't worry about it). Current code, commented with things I'm not happy about: https://gist.github.com/flaghacker/bfc6c09e50bacdbb236e79b8729ffe24
e

Eugenio

09/29/2018, 3:52 PM
A few questions: * What exactly is User? * Why do you need Value to be User? * Why not make things immutable?
k

karelpeeters

09/29/2018, 3:58 PM
• User is "something that uses values", it only has that single function defined on it. This comes up in optimization: let's say someone uses
Add(Constant(1), Constant(2))
as an operand, that operand can be replaced with
Constant(3)
everywhere in the IR. In practice this would be a
replaceWith(Constant(3))
call on the add instruction I mentioned above, that then loops over the users and replaces itself with the new value. • I specifically don't want Value to be a User, see the comment above Value. That is just my current crappy solution to the fundamental multiple inheritance problem: things can be Values and Users at the same time. • Because the optimizations mutate stuff all over the place, eg. in the above example replacing
1 + 2
with
3
requires mutating of everything that uses
1 + 2
. Creating so many new objects is both bad for performance and difficult to do polymorphically (see Java's
Clonable
disaster).
e

Eugenio

09/29/2018, 4:00 PM
what I mean is: why are you currently having Value be User?
k

karelpeeters

09/29/2018, 4:01 PM
Because eg. Instruction is both a Value and a User, and multiple direct parent classes aren't allowed. Having User be a Value would be just as good/bad of a solution here.
e

Eugenio

09/29/2018, 4:02 PM
so you want Instruction to be a User
could you provide a snippet with the ideal implementation you'd want (pseudo code) and usages of things that are one and the other?
k

karelpeeters

09/29/2018, 4:14 PM
Yes, instruction is a User. Ideal implementation (User and ConstUser stayed the same): https://gist.github.com/flaghacker/79db37998107d2261fdb5a993da5c26c. I'll write up some usage examples, give me a minute.
And a basic example of how all of it should be used: https://gist.github.com/flaghacker/5c48a6005bf166376fff4ebf162599a5
(Messed up the example, should be fixed now)
e

Eugenio

09/29/2018, 5:22 PM
ok, here's a way to do it
ping @karelpeeters
k

karelpeeters

09/29/2018, 5:41 PM
Hmm, the problem is that that leaks
operator
outside of
Add
, right? I guess it's not that bad.
e

Eugenio

09/29/2018, 5:43 PM
it doesn't really
in the sense: if you put yourself in the right scope you might be able to create an instance of
OperandDelegate
, BUT that by itself is stateless!
it only does anything when
provideDelegate
is invoked, which can only be invoked on the right kind of object, and can access internals of
ConstUser
because it's enclosed in it (but it's not an inner class!)
so you could even re-use the instance of
OperandDelegate
(for a certain
Value
) across different properties and even different instructions
k

karelpeeters

09/29/2018, 5:59 PM
Oh I see I missed a couple of steps of indirection there 😒imple_smile: . I forgot about the
provideDelegate
thing, thanks! I used to cache the delegate instances but I cut out that part to have simpler code. I'll toy around a bit with this.
val zero = Constant(0)
val x = Add(zero, zero)
with(x) { x.operand(zero) }
but that's pretty unlikely.
e

Eugenio

09/30/2018, 6:04 PM
@karelpeeters yes but that's what I mean: you can do that, but creating an instance won't do anything, you only do "something" when you use it as a property delegate with
by
and you can only do that in the correct type, plus it won't affect the instance that created the delegate, but the instance on which the delegate is applied
k

karelpeeters

09/30/2018, 6:45 PM
Right it won't do anything bad, that's true.