Re:Proposal: expression to create pointer to simple types
by gowld on Monday April 19, 2021 @10:42AM
(#26863256)
Nope. You can only take a reference to an lvalue, which is (essentially) an expression that is legal to use in the form `my_lvalue = ....
Otherwise, there's nothing to take the reference of.
You can still be unsafe though, by making a reference to a stack-allocated object and letting it go out of scope :
[ Parent
| on HN ]
Re:Proposal: expression to create pointer to simple types
by masklinn on Monday April 19, 2021 @11:17AM
(#26863675)
> You can only take a reference to an lvalue, which is (essentially) an expression that is legal to use in the form `my_lvalue = ….
I mean it could implicitly allocate, that's what Rust does for instance.
Your second and third attempts would not compile though, the first would by returning a `&'static T`.
Re:Proposal: expression to create pointer to simple types
by steveklabnik on Monday April 19, 2021 @11:55AM
(#26864187)
"implicitly allocate" is slightly misleading, imho. It's promoted to a static. There's no malloc involved.
Re:Proposal: expression to create pointer to simple types
by masklinn on Monday April 19, 2021 @12:02PM
(#26864275)
> "implicitly allocate" is slightly misleading
Maybe. I just meant that storage is created implicitly (static or stackframe depending on the case), then a reference is created to that.,
Re:Proposal: expression to create pointer to simple types
by steveklabnik on Monday April 19, 2021 @12:40PM
(#26864775)
Totally, I don't think you're wrong, just like, people read "allocate" in different ways. I wish words were clearer, heh.