Skip to content
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

Initialize on access #8

Open
KristofferC opened this issue Jun 9, 2021 · 4 comments · May be fixed by #39
Open

Initialize on access #8

KristofferC opened this issue Jun 9, 2021 · 4 comments · May be fixed by #39

Comments

@KristofferC
Copy link
Owner

Could be nice to support adding syntax so that:

compute() = 2

@lazy struct Foo
    x::Int = 3
    @lazy field::Int = compute()
end

f = Foo()
f.field

would initialize field with the function compute.

@DilumAluthge
Copy link
Contributor

DilumAluthge commented Jan 11, 2022

This would be a really nice feature.

Would it make sense for the computation function to take the (partially-initialized) f as input?

So, e.g., something like this:

function compute_double end
@lazy struct LazyDoubler
    x::Int
    @lazy double_of_x::Int = compute_double
end
LazyDoubler(x::Int) = LazyDoubler(x, uninit)
compute_double(foo::LazyDoubler) = foo.x + foo.x

bar = LazyDoubler(3)
bar.double_of_x # this will be computed on-demand, and the result will be 6

@Tortar
Copy link

Tortar commented Jan 7, 2024

I wonder if this is not better:

@lazy @kwdef struct Foo
    x::Int = 3
    @lazy field::Int = compute()
end

I also think that indeed adding @kwdef somewhere inside the macro could solve the issue maybe

@Tortar
Copy link

Tortar commented Jan 7, 2024

actually the @kwdef macro requires keywords argument for the unitialized fields so it is not the same feature requested here, but I think that making @lazy compatible with @kwdef is nonetheless a good feature

@BambOoxX
Copy link

BambOoxX commented Mar 12, 2024

I am not sure if this is related, but would it make sense to support get(<lazystruct>,:<lazyprop>,<lazydefault>) when isinit(<lazystruct>,:<lazyprop>) == true returns <lazyprop> when false returns <lazydefault> ?

@TomLemmensSTN TomLemmensSTN linked a pull request Aug 8, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants