Closed Lysander closed 1 month ago
I just had a look at your PR and noticed the following: If a Tag
is used as the default value, it will always be rendered, no matter whether it is actually needed.
As a fix, I would like to propose an overloaded export
function with a getter function instead of a static value (or at least an option to have one):
fun export(default: () -> T, scope: Exporter<T>.() -> Unit): T
@haukesomm Good one! exactly this lead to the solution to simpley remove this function! I have updated the PR (just deleting the files) and the PR comment to explain our decision.
Problem
Until this PR, the
export
function was not safe to use:As you can see there is the
lateinit va payload: T
, which gets only set, ifexport()
-function gets calles in theinitialize
-Lambda.This cannot be forced! And that leads to a brittle API :-(
Imagine some control structures like
if
, which prohibits the call if some value fails its condition. Then the outerexport
call will try to access thepayload
-field in order to return it. But it is still not initialized:Solution
We have decided to remove this function from fritz2!
It is simply not possible to reach for a good design, that would fit all kind of situations.
One idea could be to introduce some
default
-parameter to cover the case, where noexport
is called inside theExporter
-scope. But this would force people to always provide some senseless default value, that will never be used in the end. As this function is often used to return someTag<*>
from a nested UI-code section, one would have to create an "empty"-tag-object, which will be rendered inside the DOM anyways...You can guess, there is no good default - that's probably why there is nothing like this built into Kotlin's standard lib ;-)
As you can see this is unfortunately API breaking, but there was no other solution.
Migration Guide
Thus said, it should be quite simple to bypass the use, by just
If you have code, where it is not guarantueed, that the
var
will be initialized, you cant uselateinit
, but should rather use a "normal"var
oder make the type of thevar
nullable like this:fix #878