August 22, 2008 Request for forced implicit cast | ||||
|---|---|---|---|---|
| ||||
When it comes to overloaded functions where not all arguments match exactly, they must be casted to the arguments types of the method that should be used.
But when cast(Type) is applied to the arguments and code changes later on, it can happen the cast hides a compiler error. Because the argument would no more be implicit castable to the methods arg type.
This is why i would like to have 'soft' cast, that does only help the compiler to choose which method I want, but only is allowed for implicit casts.
soft_cast(Type)
implicit_cast(Type)
Or should this be done with templates?
template soft_cast(T){
T soft_cast(U)( U u ){
return u; // implicit cast to return type
}
}
The problem here is, that no error is given for the place of
instantiation, i think. And the syntax is less nice.
soft_cast!(Type)(ref)
| ||||
August 22, 2008 Re: Request for forced implicit cast | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Frank Benoit | Frank Benoit:
> When it comes to overloaded functions where not all arguments match exactly, they must be casted to the arguments types of the method that should be used.
Or you may modify your functions:
int foo(T1, T2)(T arg1, T2 arg2) {
static if (Is!Type(T1, int, long, short) && IsType!(T2, string)) {
do something
} else {
do something different
}
}
In D 2.x recently there's a new syntax to do similar things in a default way.
Bye,
bearophile
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply