https://kotlinlang.org logo
#library-development
Title
# library-development
m

mbonnin

03/24/2021, 4:37 PM
What's everyone take on extension functions? Is there a convention when to use 1. 
foo.bar()
2. 
foo.asBar()
3. 
foo.toBar()
4. 
foo.withBar()
I'd tend to use mostly 1. with maybe 4. when it returns the same type as the receiver but I'm curious if there are established guidelines when to use what?
e

ephemient

03/24/2021, 4:54 PM
wrapper =
.asBar()
, new =
.toBar()
6
e.g.
Copy code
val a = arrayOf(1, 2, 3)
val b = a.asList()
val c = b.toList()
a[2] = 4
b == listOf(1, 2, 4)
c == listOf(1, 2, 3)
👍 1
m

mbonnin

03/24/2021, 5:05 PM
Makes sense...
Trying to do everything as immutable as possible I guess I won't use
.asBar()
much
l

louiscad

04/03/2021, 10:49 AM
When wrapping something immutable, the
toBar()
one can still wrap instead of doing a copy.
e

ephemient

04/03/2021, 5:20 PM
yep, although if with some use patterns you might be better off with a copy anyways so the original can be garbage collected
2 Views