Closed arucil closed 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 .
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!
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.
The reason is that:
Using OnceCell
directly should always work, as it doesn't require naming the type, see this example:
Thanks for the quick response @matklad. I'll make another attempt using OnceCell
then.
I have the following code
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 tofn() -> T
. But I don't know any way to explicitly specify the closure type parameter. So how can I make this code working?