Like in Swift, can we somehow shorten `Column(hori...
# compose
a
Like in Swift, can we somehow shorten
Column(horizontalAlignment = Alignment.CenterHorizontally)
to?
Column(horizontalAlignment = .CenterHorizontally)
to improve readability? It would be a, seemingly, simply thing but would increase the readbility to codebases massively.
🚫 2
j
you can import
CenterHorizontally
like so:
Copy code
import androidx.compose.ui.Alignment.Companion.CenterHorizontally
then use it directly:
Copy code
Column(horizontalAlignment = CenterHorizontally)
👍 4
a
Inspired by your idea I've just made my codebase about 3000 times more readable by using this:
Copy code
@Composable fun surface() = MaterialTheme.colors.surface
@Composable fun primary() = MaterialTheme.colors.primary
@Composable fun onPrimary() = MaterialTheme.colors.onPrimary
@Composable fun secondary() = MaterialTheme.colors.secondary
@Composable fun onSecondary() = MaterialTheme.colors.onSecondary
@Composable fun background() = MaterialTheme.colors.background
@Composable fun onBackground() = MaterialTheme.colors.onBackground
Things like this mean I don't need to modify the imports and assume Android Studio will know what I want to import:
Copy code
val CenterHorizontally = Alignment.CenterHorizontally
val CenterVertically = Alignment.CenterVertically
j
You can even push this a little further and make colors computed values by annotating getter with
@Composable
like so:
Copy code
val primary: Color
    @Composable get() = colors.primary
I consider using
colors.primary
a bit more readable than plain
primary
/`primary()` , but that’s for sure a matter of preference 👍
🙌 1
a
Yeah I've just done the same this with
MaterialTheme.typography
.
z
There’s an issue somewhere proposing this as a language feature, I’d link it but I can’t find it
j
I found these two discussions a while back. I am not sure whether there is some technical challenge, but it seems to me the community is not interested in such feature. Btw, linked discussions are about leading dot notation for enums, but it’s a far more powerful feature in swift
o