Libraryless. Click here for Pure Java version (5414L/32K).
sclass LazyVar<A> implements IF0<A> { *() {} *(A *v) { if (v == null) calculating = new Var; } *(IF0<A> *f) {} IF0<A> f; // will keep var around after the fact (we could change this by counting how many threads are waiting) Var<OKOrError<A>> calculating; A v; public A get() { bool meCalc; if (v != null) ret v; synchronized { meCalc = calculating == null; if (meCalc) calculating = new Var; } if (meCalc) { OKOrError<A> b = okOrError(() -> f!); calculating.set(b); synchronized { ret v = b!; } } else { waitUntilNotNull(calculating); ret v; } } public synchronized bool isEvaluated() { ret f == null; } public synchronized bool has() { ret get() != null; } toString { ret isEvaluated() ? str(get()) : "LazyVar"; } }
Began life as a copy of #1003550
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1032743 |
Snippet name: | LazyVar - variable evaluating itself on demand |
Eternal ID of this version: | #1032743/10 |
Text MD5: | 12fdd02d55a9bf15e46aa46e264a14ae |
Transpilation MD5: | cc04de52f3b5ad61375236fdb81b3d2d |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-10-04 07:23:36 |
Source code size: | 886 bytes / 38 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 175 / 324 |
Version history: | 9 change(s) |
Referenced in: | #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674) |