People playing with newtypes and refinement types ...
# getting-started
u
People playing with newtypes and refinement types etc, how do you call the factory function? Is there a convention?
Copy code
@JvmInline
value class Percentage private constructor(val value: Int) {
    companion object {
        fun ???(value: Int): Percentage? =
            if (value in 0..100) Percentage(value) else null
    }
}
of
?
create
? rusty
new
?
h
I prefer a top level function with the same name
u
Doesn't that make your constructor
internal
?
h
Oh yeah, that's right 🫠
u
also, doesnt the style linter complain about capitalization?
h
You could add an operator invoke function if you want to keep the signature
y
Yeah I do
operator fun invoke
, or if you want to maintain the private constructor but also have a top-level factory function, I do a companion
internal fun construct
or similar, then a top level function of the same name
u
about operator
invoke
, I find it search is extremely slow for some reason, and unreliable at times, dont you? something is indexed differently than a normal function for sure
about the top levle functions, what do you do about the naming warning? supress locally?
y
Definitely suppress the name warning Re invoke search, I've had some issues in the past, but I don't recall experiencing that recently; definitely report it if you can reproduce it; it makes sense why it'd struggle,
invoke
is very implicit, so it has to search in strange places
u
yea its a asecond class citizen, for example when you
class Foo : Function .. { override fun invoke
I cannot just cmd click on the invoke to find the callsite, that flat out doesnt work
btw whats your reasoning for top level + internal vs s companion and private? same callsite shape?
e
If a class is not as small as the one in your example, that is if a class contains lots of fields, our team prefers to use a mapper as a component injected instead. This way mapping logic belongs to non-static context
u
this is more about invariant enforcement
👍 1
"I have instance A, and therefore I know it cannot contain illegal configuration"
👍 1
(OT: don't inject mappers if they're stateless, instances are the wrong tool here, unless you explicitly have the need for variance in the logic)
e
The thing is that now they can be stateless. But it is hard to predict the future. At some point of time in the future some dependency needs to be passed
And you end up threading dependencies or refactoring to instance mappers. So, it is wise to have instance mappers in the first place
That way fully prepared whatever comes in the future. Paying zero cost
Doesn't it make sense?
u
not really but maybe your philosophy is different
👌 1
a
> also, doesnt the style linter complain about capitalization? It doesn't for me. https://kotlinlang.org/docs/coding-conventions.html#names-for-class-like-functions But you might need to explicitly state the return type.
u
thas a interface
y
I have used
internal
+
OptIn
before to similar level of success tbh. All it takes is just finding usages of the opt-in. But I agree, I'd use
private
constructor first, and only relax it to
internal
+
OptIn
if need be