What's everyone take on extension functions? Is th...
# library-development
m
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
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
Makes sense...
Trying to do everything as immutable as possible I guess I won't use
.asBar()
much
l
When wrapping something immutable, the
toBar()
one can still wrap instead of doing a copy.
e
yep, although if with some use patterns you might be better off with a copy anyways so the original can be garbage collected