Deduce template arguments from return value?

Idan Arye via Digitalmars-d digitalmars-d at puremagic.com
Sun Jul 12 05:52:44 PDT 2015


On Sunday, 12 July 2015 at 09:13:03 UTC, Yuxuan Shui wrote:
> For example:
> import std.conv;
> T a(T)(int a) {
> 	return to!T(a);
> }
> void main(){
> 	string x = a(2);
> }
>
> D is not able to deduce T. Can we make it possible to deduce 
> template arguments from where the return value is assigned to?
>
> Rust is able to do this:
> fn main() {
> 	let a : Vec<i32> = Vec::new();
> }
>
> (In fact, you can even do this is Rust:
> fn main() {
> 	let mut a = Vec::new();
> 	a[0] = 0i32;
> })

Just like ML, Rust's amazing type inference comes with a price - 
a super strict type system. D has less strict type system, which 
allows - for example - implicit conversions in some 
cases(consider http://dpaste.dzfl.pl/ed83a75a48ba)

For D to support Rust's kind of type inference, it's type system 
will need to be completely replaced with something much more 
strict. Whether you think such type systems are good or not - 
this change will result in a massive code breakage.


More information about the Digitalmars-d mailing list