https://kotlinlang.org logo
#compose
Title
# compose
j

James Ward

05/14/2019, 4:15 PM
Hi all, I've been learning about Compose and found the docs make an incorrect claim that they are pure functions: https://android.googlesource.com/platform/frameworks/support/+/androidx-master-dev/compose/README.md I've sent a pull request to fix that: https://android-review.googlesource.com/c/platform/frameworks/support/+/958319 Would be great to get someone to review that.
👍 5
c

Chuck Jazdzewski [G]

05/14/2019, 4:20 PM
+2
j

James Ward

05/14/2019, 4:20 PM
Thanks!
j

Jeremy

05/14/2019, 4:33 PM
Yes I was also thinking that during presentation
c

Chuck Jazdzewski [G]

05/14/2019, 4:37 PM
The point we were trying to make is that the only side-effect the function should have is producing the tree. There isn't really a well understood term for that. The closest is monad but then only 5 people know what that means.
☝️ 4
j

Jeremy

05/14/2019, 4:57 PM
Why not: fun Greeting(name: String): Composable { return Text(text = "Hello $name") }
🚫 1
💯 1
😱 1
t

themishkun

05/14/2019, 5:28 PM
@Chuck Jazdzewski [G] partially pure functions, then?
👍 1
g

ghedeon

05/14/2019, 5:28 PM
The closest is monad but than only 5 people know what that means
But who would think that so many people know what pure function means and now they are giving you hard time for not being transparent... referentially 🧌
r

romainguy

05/14/2019, 5:33 PM
@themishkun Pragmatically pure :)
simple smile 5
@ghedeon James doesn't count, he's a fan of Scala :)
👍 3
t

themishkun

05/14/2019, 5:35 PM
@romainguy yeah, kotlin fans LOVE the word "pragmatically" :)
c

Chuck Jazdzewski [G]

05/14/2019, 5:37 PM
@Jeremy That would work and is the approach take by React and Flutter. However, there is more opportunity for optimization along the line we took. For example if "name" is the same on recomposition we can skip the call entirely. We don't need to compare its result from last time to tell that it doesn't change. This is similar to the approaches taken by, for example, Ember and Angular.
🤔 1
2
☝️ 2
t

themishkun

05/14/2019, 5:42 PM
@Chuck Jazdzewski [G] is this really takes so much work to compare two `id`s in two generated data structures? In my team most screens consist of root recyclerviews with adapters for buttons, galleries, texts and titles, etc. and we see no perfomance impact on that.
j

Jeremy

05/14/2019, 5:48 PM
🤔 I think I just need to dig into more. Its just less clear from example what its actually doing
c

Chuck Jazdzewski [G]

05/14/2019, 5:48 PM
The
RecyclerView
is doing a lot of work behind the scenes to deliver this performance.
j

Jeremy

05/14/2019, 5:52 PM
Is there any examples of unit tests? If multiple tests are executing does any global state need to be reset?
👍 1
c

Chuck Jazdzewski [G]

05/14/2019, 6:08 PM
@Jeremy There are many unit tests in the repository and they can be found in the
androidTest
directory of the various modules. The memoization information is stored in the composer's slot table and is local for the composition. It is not global. However, currently we use a global to simplify the code generation of composable functions so only one composition can be occurring at a time. We plan to change this to rewrite composable functions to take the composer as a parameter. This will remove global as well as speed up composition.
j

Jeremy

05/14/2019, 6:29 PM
Thanks for response. I'll dig through the androidTest code a bit more
So api would be : fun Greeting(name: String, compose: Composable) ?
c

Chuck Jazdzewski [G]

05/14/2019, 6:40 PM
There will be a synthetic function produced with the parameter. The API will remain that same at the source level. We will redirect calls to composable functions to the synthetic.
👍 1
j

James Ward

05/15/2019, 1:53 AM
Sorry I missed the chatter here. It's definitely not monadic either. I don't think we should try to use any FP terms since the FP zealots will freak out. I'm not quite a zealot, just an advocate of using terms correctly.
I don't know what is really going on with Composable but it does seem like the API could use an IO Monad instead but you would need a return statement which I'll admit doesn't seem as nicely declarative as what exists today.
12 Views