matklad / once_cell

Rust library for single assignment cells and lazy statics without macros
Apache License 2.0
1.87k stars 109 forks source link

How to initialize Lazy with a closure? #90

Closed arucil closed 4 years ago

arucil commented 4 years ago

I have the following code

fn main() {
  use once_cell::unsync::Lazy;
  let i = 1;
  let x: Lazy<i32> = Lazy::new(|| i);
}

Rustc reports that a fn pointer was expected, but a closure is found. I guess it is because the type parameter T of Lazy is defaulted to fn() -> T. But I don't know any way to explicitly specify the closure type parameter. So how can I make this code working?

matklad commented 4 years ago

Use Lazy<i32, _>, or omit the type altogether.

On Saturday, 18 January 2020, plodsoft notifications@github.com wrote:

I have the following code

fn main() { use once_cell::unsync::Lazy; let i = 1; let x: Lazy = Lazy::new(|| i); }

Rustc reports that a fn pointer was expected, but a closure is found. I guess it is because the type parameter T of Lazy is defaulted to fn() -> T. But I don't know any way to explicitly specify the closure type parameter. So how can I make this code working?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/matklad/once_cell/issues/90?email_source=notifications&email_token=AANB3M3DU76HYDEDP3NPTDDQ6KYHRA5CNFSM4KIRVHB2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4IHC2OMQ, or unsubscribe https://github.com/notifications/unsubscribe-auth/AANB3M4JT3WU6KXKAG3X6S3Q6KYHRANCNFSM4KIRVHBQ .

arucil commented 4 years ago

Use Lazy<i32, _>, or omit the type altogether. On Saturday, 18 January 2020, plodsoft @.***> wrote: I have the following code fn main() { use once_cell::unsync::Lazy; let i = 1; let x: Lazy = Lazy::new(|| i); } Rustc reports that a fn pointer was expected, but a closure is found. I guess it is because the type parameter T of Lazy is defaulted to fn() -> T. But I don't know any way to explicitly specify the closure type parameter. So how can I make this code working? — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#90?email_source=notifications&email_token=AANB3M3DU76HYDEDP3NPTDDQ6KYHRA5CNFSM4KIRVHB2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4IHC2OMQ>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AANB3M4JT3WU6KXKAG3X6S3Q6KYHRANCNFSM4KIRVHBQ .

Lazy<i32, _> works for me, thank you!

pemistahl commented 4 years ago

The trick using _ does not work with global and function type definitions. Why is it prohibited to use closures most of the time? Are there technical reasons? This restriction makes this library useless to me, unfortunately. Languages such as Kotlin allow that.

matklad commented 4 years ago

See https://github.com/matklad/rfcs/blob/std-lazy/text/0000-standard-lazy-types.md#default-type-parameter-on-lazy.

The reason is that:

Using OnceCell directly should always work, as it doesn't require naming the type, see this example:

https://github.com/matklad/once_cell/blob/0b65b1523453a0cfbdeeb756a77e0b85e03ea7bf/examples/lazy_static.rs#L14-L24

pemistahl commented 4 years ago

Thanks for the quick response @matklad. I'll make another attempt using OnceCell then.