June 12, 2012 Template argument types | ||||
---|---|---|---|---|
| ||||
Do you know why D templates accept floating point values and even arrays of 32 bit dchars: template Foo(dchar[] s) { enum size_t Foo = s.length; } void main() { pragma(msg, Foo!("hello"d.dup)); } But they don't accept arrays of ints? template Foo(int[] s) { enum size_t Foo = s.length; } void main() { enum int[] a = [1, 2, 3]; pragma(msg, Foo!a); } ===> test.d(1): Error: arithmetic/string type expected for value-parameter, not int[] This works, but it's not the same thing: template Foo(alias s) if (is(typeof(s) == int[])) { enum size_t Foo = s.length; } void main() { enum int[] a = [1, 2, 3]; pragma(msg, Foo!a); } Bye and thank you, bearophile |
June 12, 2012 Re: Template argument types | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 12-06-2012 02:04, bearophile wrote: > Do you know why D templates accept floating point values and even arrays > of 32 bit dchars: > > template Foo(dchar[] s) { > enum size_t Foo = s.length; > } > void main() { > pragma(msg, Foo!("hello"d.dup)); > } > > > But they don't accept arrays of ints? > > > template Foo(int[] s) { > enum size_t Foo = s.length; > } > void main() { > enum int[] a = [1, 2, 3]; > pragma(msg, Foo!a); > } > > > ===> > test.d(1): Error: arithmetic/string type expected for value-parameter, > not int[] > > > This works, but it's not the same thing: > > template Foo(alias s) if (is(typeof(s) == int[])) { > enum size_t Foo = s.length; > } > void main() { > enum int[] a = [1, 2, 3]; > pragma(msg, Foo!a); > } > > Bye and thank you, > bearophile My guess is that dchar[] is special-cased in the compiler to allow passing strings. I think we should extend this to support arrays of all primitive types. -- Alex Rønne Petersen alex@lycus.org http://lycus.org |
Copyright © 1999-2021 by the D Language Foundation