Inferred Type for Explicit Cast
Jonathan Marler via Digitalmars-d
digitalmars-d at puremagic.com
Thu Dec 18 14:46:04 PST 2014
What are peoples thoughts on having an inferred type for "cast"?
Good/Bad idea? If good, how helpful would this be? Would this
break existing code somehow? I think this feature would be a nice
added convenience. Not super helpful but nice to have.
Here's the details
-------------------
Typed Cast: cast(T)v
try to cast v to T
Type Inferred Cast: cast(auto)v
try to cast v to whatever type is required in the current
context
void foo(string s)
{
// ...
}
void main()
{
const(char)[] s = "hello";
foo(cast(string)s); // Current
foo(cast(auto) s); // The type of the cast is inferred to be
a string
foo(cast() s); // Another possible syntax
}
This would help refactoribility. If a function argument changes
it's type, and the caller is using a cast, then the caller's cast
type will be updated automatically. Note that if it changes to
an invalid type, the cast will still fail like normal.
More information about the Digitalmars-d
mailing list