www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Template return type?

reply JN <666total wp.pl> writes:
I was looking over some Rust examples, and found something 
interesting:

https://github.com/serde-rs/serde

let deserialized: Point = 
serde_json::from_str(&serialized).unwrap();

from_str is defined as:

pub fn from_str<'a, T>(s: &'a str) -> Result<T>

 From what I understand, the function infers the template type 
from the expected return type. Is it possible to achieve 
something like that in D?

I tried something like:

T returnDefault(T)()
{
     return T.init;
}

int i = returnDefault();

but it doesn't work.

int i = returnDefault!int(); obviously works, but the point is I 
would like to skip the explicit type.
Oct 03 2018
parent reply Andrea Fontana <nospam example.com> writes:
On Wednesday, 3 October 2018 at 09:37:29 UTC, JN wrote:
 int i = returnDefault!int(); obviously works, but the point is 
 I would like to skip the explicit type.
See: https://forum.dlang.org/post/ufhibwmouxpivjylqdgm forum.dlang.org
Oct 03 2018
parent Sebastiaan Koppe <mail skoppe.eu> writes:
On Wednesday, 3 October 2018 at 10:01:02 UTC, Andrea Fontana 
wrote:
 On Wednesday, 3 October 2018 at 09:37:29 UTC, JN wrote:
 int i = returnDefault!int(); obviously works, but the point is 
 I would like to skip the explicit type.
See: https://forum.dlang.org/post/ufhibwmouxpivjylqdgm forum.dlang.org
See also: https://forum.dlang.org/post/qmmjiofwmzqzdsgmvrai forum.dlang.org
Oct 03 2018