-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: lazy Val/Var #57
Comments
Hi, if you just need a lazy value, you don't need a If you want to wrap a |
Hi, thanks for the hint. I just checked your code. I did not realize I still want to make something clear. I think it's what you wanted to tell me, but for anyone else checking in I want to be a bit more explicit.
While technically true, it will still potentially reevaluate the if(!valid || !isObservingInputs()) {
value = computeValue();
valid = true;
} As long as no observer is registered, the But I'm mostly interested in the second, async version. In my project, we need to load a lot of rather large objects and their dependencies from a remote server, over a bad VPN connection. I want to utilize the whole focus on So I will probably write both methods anyway. Question is, is there any interest in this out there? It's your framework, so ultimately your choice. Consider it a free offer. :) EDIT: I've just found a nice use case for a lazy |
Sorry, I meant "memoizing or not." But you probably figured that out.
If you want the loading to be asynchronous, what would you return from
I don't quite understand. You still need to inject the value somewhere, so something else will have to be non- |
True, you need to supply a placeholder. For a String, for example, something like "loading...".
Not necessarily. Consider this: @Inject
private SomeService service;
private final Val<String> serviceResult = Val.constant(service.calc()); Depending on the framework, @Inject
private SomeService service;
private final Val<String> serviceResult = Val.lazy(() -> service.calc()); This will work. Only when PS: I didn't say it yet, but I will say it now: Thanks for a great farmework. Not just the API and features, also the codebase. It's really well structured, even more so when compared to the mess that is JavaFX. |
That still can be done by Val.create(memoize(() -> service.calc())); where static <T> Supplier<T> memoize(Supplier<T> supplier); So the convenience for the user of your proposed method is not having to implement that |
For error handling, I would give the same arguments you had when it came to errors during event stream processing. Either you handle it directly inside the I will prepare a pull request, so that we can discuss the async version a bit easier. I'll implement it after we agreed on the API. |
Exceptions that are programming bugs should bubble up. But asynchronous task are often associated with inherently unsafe operations like (network) I/O. Those should be reflected in the return type. So perhaps let the async version(s) return |
Hi,
I'm thinking about an extension to
Val
/Var
, or rather to the provided factory methods. The idea is to provide aSupplier
that gets called the first time the value is actually read.This is obviously useful for cases where a default value is expensive to calculate. But I was actually thinking about loading remote values on demand. Instead of loading everythink immediately, this would provide a clean API for lazy loading. For remote calls, this would have to be done async.
While it's easy enough for a
Val
, because it cannot change, I'm not so sure how or even if it's useful for aVar
. But I would be grateful for any input on this. If there is interest, I would be happy to code it myself and start a pull request.The text was updated successfully, but these errors were encountered: