Recently, I made a pre-rfc about potential optional fields in methods. The general feedback was that the syntax should be more generic, and be applicable to more places.
This is one idea that stemmed from it, which I think would be quite useful in a variety of cases. There's another one that I'm still debating whether to do, as it was suggested lots to split it into two parts (it's
sugar for Option
)
Into syntactic sugar
You can use the postfix !
operator to automatically .into()
an expression.
Uses
- (Directly from the original pre-RFC) Configuration structs, or structs with many
Option
s. When using these with struct literals, doingfoo: 3!
is a lot more readable imo and less verbose thanfoo: Some(3)
- Functions that take in
Into
as an argument, to accept more types. This means, in the function, you can use the input parameter with!
. I'm also considering whether we should have this operator in patterns, so a function could takefoo!: Into<Bar>
, say, andfoo
would be of typeBar
- Functions that don't take
Into
as an argument, to convert your types without specifying.into()
everywhere
Rationale
- Signifies intent clearly
- Does not reduce readability
Won't this get confused with macros?
This was one of my concerns when I thought of this idea; however, macros can only be called with brackets afterwards. In most cases where you'd use this, you wouldn't be specifying brackets afterwards, and as such, that will attempt to invoke a macro instead.
Of course, if there's a more suitable operator to use, please say.