| Thread overview | ||||||||
|---|---|---|---|---|---|---|---|---|
|
October 31, 2013 howto make a function get a types form compilte-time-parameters AND from the arguments ? | ||||
|---|---|---|---|---|
| ||||
(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
| ||||
October 31, 2013 Re: howto make a function get types form compile-time-parameters AND from arguments ? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Uplink_Coder | 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]); | |||
October 31, 2013 Re: howto make a function get types form compile-time-parameters AND from arguments ? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Uplink_Coder | 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)
| |||
October 31, 2013 Re: howto make a function get types form compile-time-parameters AND from arguments ? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to John Colvin | 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 | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply