Thread overview
Template return type?
Oct 03, 2018
JN
Oct 03, 2018
Andrea Fontana
Oct 03, 2018
Sebastiaan Koppe
October 03, 2018
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.
October 03, 2018
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

October 03, 2018
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