digitalmars.D.learn - Template return type?
- JN (19/19) Oct 03 2018 I was looking over some Rust examples, and found something
- Andrea Fontana (3/5) Oct 03 2018 See:
- Sebastiaan Koppe (4/9) Oct 03 2018 See also:
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
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
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:See also: https://forum.dlang.org/post/qmmjiofwmzqzdsgmvrai forum.dlang.orgint 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