Closed as not planned
Description
Nim should support partial generic deduction.
-
this matters, eg for allowing
"true".to[bool]
(otherwise it's not DRY, eg:"true".to[bool, string]
), cf https://github.com/nim-lang/Nim/issues/7430 and other use cases -
if partial generic deduction is not on the roadmap, it will cause https://github.com/nim-lang/Nim/issues/7517 ([RFC] guidelines for when to use typedesc vs generics) to be in clear favor of using typedesc, which, as discussed there, is not ideal.
# D20180406T122342
import typetraits
proc test_IFTI() =
## full IFTI
proc foo[T](a:T): auto = return a*a
echo foo 10
## partial IFTI
### proc
proc foo1[T, T2](a:T2): auto = return T.name & ":" & T2.name
echo foo1[string, int](10)
when false:
echo foo1[string](10) # Error: cannot instantiate: foo1[string]; got 1 type(s) but expected 2
echo 10.foo1[string]() # Error: cannot instantiate: 'T'
## side issue: named generic parametes
when false:
echo foo1[T:string](10) # Error: invalid expression: 'T: string'
echo foo1[T:string, T2:int](10) # Error: invalid expression: 'T: string'
echo 10.foo1[T:string]() # Error: cannot instantiate: 'T'
### template
template foo2[T, T2](a:T2): auto = T.name & ":" & T2.name
echo foo2[string, int](10)
echo foo2[string](10)
when false:
echo 10.foo2[string] # Error: cannot instantiate: 'T'
## workaround with typedesc
template foo3[T2](a:T2, T: typedesc): auto = T.name & ":" & T2.name
echo 10.foo3(string)
test_IFTI()
in C++ this works:
template<typename T, typename T2>
void fun(const T2& a){}
void test(){
fun<float>(10);
}
in D this works and used extensively
void fun(T, T2)(T2 a){}
void test(){
fun!string(10); // same as fun!(string)(10)
}