digitalmars.D.learn - howto make a function get a types form compilte-time-parameters AND
- Uplink_Coder (17/17) Oct 31 2013 (this is my first post so please alert me to any breaches of
- Uplink_Coder (5/5) Oct 31 2013 Maybe I shuold be more specific :
- John Colvin (11/16) Oct 31 2013 void foo(T,U)(U arg){}
- Uplink_Coder (9/9) Oct 31 2013 by changeing
(this is my first post so please alert me to any breaches of forum etiquette) Hello, I have a problem with a helper function I'm writeting. T[] makeArray(T,U)(immutable U[] paramArray) { T[] result; foreach (param;paramArray) { result ~= T(param); } return result; } and I would like not to take the compile-time-parameter U but insead infering it form the argumet. is it possible ? and if. how ? ---- Uplink_Coder
Oct 31 2013
Maybe I shuold be more specific : for now It's: T[] arr = makeArray!(T,U)([some,instances,of,U]); but I would like it to be: auto arr = makeArray!(T)([some,instances,of,U]);
Oct 31 2013
On Thursday, 31 October 2013 at 10:18:25 UTC, Uplink_Coder wrote:Maybe I shuold be more specific : for now It's: T[] arr = makeArray!(T,U)([some,instances,of,U]); but I would like it to be: auto arr = makeArray!(T)([some,instances,of,U]);void foo(T,U)(U arg){} int a; foo!(double)(a); //U is inferred as int void bar(T,U)(U[] arg){} long[] b; foo!(double)(b); //U is in inferred as long However, be aware that you can't pass mutable or const data as an immutable argument. Perhaps the signature that would work better for you would be T[] makeArray(T,U)(const U[] paramArray)
Oct 31 2013
by changeing T[] makeArray(T,U)(immutable U[] paramArray) into T[] makeArray(T,U)(const U paramArray) if (isAggregateType!(U) || is Array!(U)) everything works perfectly Thanks very much --- Uplink_Coder
Oct 31 2013