Hi folks, ```class ServerError private constructor...
# codingconventions
g
Hi folks,
Copy code
class ServerError private constructor(val message: String, val code: Int) {
    fun <T> toErrorPayload(): HubPayload<T> = errorPayload(message, code)
}
In this case the function
toErrorPayload
(for the sake of example ignore the HubPayload return type :)) naming prefix convention shouldBe
to
or
as
. Since im returning another object i guess the default is
to
, but if i want to emphasize that the values remain the same, just the wrapper is changing then should i name it
asErrorPayload
? Thanks in advance for any answers !
j
Generally I use
to
, I only use
as
when
to
is already there, i.e,
toString()
. I have seen this behavior in some popular libs but I would like to know more opinions too
p
Semantically I'd see
as
as a view of the value in the alternative shape with
to
being a copy in the alternative shape.
When it comes to immutable structures there's no real difference and I'd lean towards
to
. As an aside I'd probably declare it as an extension method unless the conversion truly belongs to the class.