hfhbd
07/19/2021, 11:03 AMInt/Double/String/LiteralExpression
feature to Kotlin? Ideally, this would be an interface, maybe even restricted to the compiler only. This would allow you to limit some helper functions to literals only, and don't pollute the namespace with helper methods.
Sample
val IntLiteral.seconds: Duration get() = ...
fun getX() = 42
val secs = 2.seconds // valid, as 2 is a constant literal
val secs2 = getX().seconds //invalid, seconds can only be called from a IntLiteral
Swifts similar implementation: https://developer.apple.com/documentation/swift/swift_standard_library/initialization_with_literalsdmitriy.novozhilov
07/19/2021, 11:05 AMhfhbd
07/19/2021, 11:13 AMhfhbd
07/19/2021, 11:15 AMval Int.px: CSSLenght get() = ...
val height = 10.px
dmitriy.novozhilov
07/19/2021, 11:20 AM10.px
calls which are semantically same and you want to turn them all to smth like this:
const val DEFAULT_HEIGHT = 10
...
val heght = DEFAULT_HEIGHT.px
So, with your proposal this won't compilehfhbd
07/19/2021, 11:49 AMconst
, which is at compile time a literal (also used by optimization)dmitriy.novozhilov
07/19/2021, 11:53 AMIntLiteral
fake type will be very tricky. What about such cases?
const val IMPLICIT_CONST = 10
const val EXPLICIT_CONST: Int = 10
const val COMPILE_TIME_OPERATOR = 5 + 5
const val X = 5
const val Y: Int = 5
const val CONST_SUM = X + Y
// const funs are not part of language yet, but we are considering adding them
const fun computeHeight(): Int = ...
const val CONST_FROM_FUN = computeHeight()
In which cases SOME_CONST.px
will work and in which won't?hfhbd
07/19/2021, 12:12 PM.px
call should work for all const val
as well as const fun
, but this will results in no advantage of the limitation...hfhbd
07/19/2021, 12:13 PMconst
with this featureilya.gorbunov
07/20/2021, 2:34 PMDEFAULT_HEIGHT.px
So, with your proposal this won't compile@dmitriy.novozhilov as I understand the idea of this proposal, this shouldn't compile because there should be no such thing as
.px
property of DEFAULT_HEIGHT
dmitriy.novozhilov
07/20/2021, 2:46 PMconst val
is common usecase, and this proposal will breaks itilya.gorbunov
07/20/2021, 3:11 PMconst val DEFAULT_HEIGHT = 10.px
rnett
07/20/2021, 9:35 PM.px
property for a .toPx()
function, since imo x.px
should be x.toPx()
on non-constant values anyways (for readability, mostly). Then any refactorings can just update to the non-literal version.elizarov
07/29/2021, 9:29 PM