Proposal to restrict an extension function for a `...
# language-proposals
b
Proposal to restrict an extension function for a
typealias
to that explicit
typealias
. Even if the compiler later removes all type aliases they could (and should?) first be enforced.
Copy code
typealias Degree = Double
typealias Radian = Double

fun Degree.toRadian():Radian = // Implementation
fun Radian.toDegree():Degree = // Implementation

fun run(degree:Degree) {
  // should only be able to call toRadian and not toDegree on this parameter.
  degree.toDegree()
}
l
@bob Please add your use case in a comment in the issue linked above
r
@bob
newtype
will solve this case
g
Yes, but newtype is bigger and more strict feature and not possible to support on JVM now. So maybe some more strict typealias would be possible to implement, just for compile type checks for Kotlin code (maybe even just compiler warnings, when you use typealias extension on original type)
1
b
@rrader Can’t find
newtype
in any documentation and can’t use it in my android project..
g
Please check comment from Andrey Breslav for this issue.
newtype
is just proposal
Oh, Dmitry Petrov answered in the issue thread and mention value types. Yeah, I just remembered that someone from Kotlin Team talked about value types and inline classes as simple case of value type (class with a single value inlined in compile time to value) as a solution for your case, when you need strict checks for types that allow to have extension and member function just for this type