Maybe we could be able to use:
- Numeric literal
- String literal
vec!
literal- Any possible future
map!
literal - Array literal
- Boolean literal
... to initialize types like Box<dyn Any>
, Rc<dyn Any>
and Arc<dyn Any>
.
As for the string literal:
At the moment this seems more benefitial for strings:
use std::any::Any;
let s: Box<dyn Any> = Box::new("some string".to_owned());
Compare this to:
use std::any::Any;
let s: Box<dyn Any> = "some string";
This means several contexts (even deep ones) will accept a string literal without much typing.
Not for non-literals
For non-literals, you continue using Box::new
or Box<_>::new
.
If this were allowed, this has some bad things:
- It hides the allocation of the
Box
,Rc
orArc
.
For literals, too, it hides the allocation of a Box
, but it's a literal: a literal is constructed on the fly, just as well as its Box
, so it doesn't hurt for Rust to support it, I guess?
For example, this seems idiomatic:
let _: Box<dyn Any> = vec! ["some string"];
This doesn't seem and wouldn't compile with the above suggestion:
let previous_vector = vec! ["some string"];
let _: Box<dyn Any> = previous_vector;
Object and Union literal
I forgot about C {}
, C
, C(...)
and (...)
literals. What do you think?
let o: Arc<dyn Any> = C1;
let o: Arc<dyn Any> = C2(item1, item2);
let o: Arc<dyn Any> = C3 {field: value};
let o: Arc<dyn Any> = (element1, element2);