https://kotlinlang.org logo
#getting-started
Title
# getting-started
e

Erik

09/02/2021, 3:27 PM
When naming an extension on some type A that returns a type B, what are the conventional names for such functions? For example, there are •
fun <T> Array<out T>.asList(): List<T>
fun <T> Array<out T>.toList(): List<T>
By signature, they are identical. So the behaviour must define the naming difference. Likewise, there exists a
fun <T> Array<out T>.asIterable(): Iterable<T>
but not
toIterable
. Then there are all kinds of variants that use names like
mapTo
. When do you use
as
,
to
,
mapTo
or something else?
d

Dominaezzz

09/02/2021, 3:32 PM
as
- is just a cheap cast/view.
to
- is a conversion.
e

Erik

09/02/2021, 3:34 PM
I see, thanks
So in a non-collection/iterable context, e.g. domain models, how would you name a function that converts a network model to a database/business logic model?
fun NetworkType.toDatabaseType(): DatabaseType
👌 3
🙏