www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - howto make a function get a types form compilte-time-parameters AND

reply "Uplink_Coder" <someemail someprovider.some> writes:
(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
parent reply "Uplink_Coder" <someemail someprovider.some> writes:
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
parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
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
parent "Uplink_Coder" <someemail someprovider.some> writes:
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