digitalmars.D.learn - Function accepts const ubyte[], const char[], immutable ubyte[],
- zabruk70 (19/19) Jan 10 2016 Hello.
- Tobi G. (21/36) Jan 10 2016 No you actually don't have to write all possible overloads.
- zabruk70 (3/4) Jan 10 2016 Tobi, big thanks!!!
- Adam D. Ruppe (11/14) Jan 10 2016 void myFunc(const(void)[] arg) {
- zabruk70 (3/4) Jan 10 2016 Ah, how i can forget about void[] !
Hello. 1st Novice question: i want function, operates sometimes with char[], sometimes with ubyte[]. internally it works with ubyte. i can use overloading: void myFunc(ubyte[] arg) {...}; void myFunc(char[] arg) { ubyte[] arg2 = cast(ubyte[]) arg; ...} It is OK. But i want 2 params (arg1, arg2), so i need write 4 overloading functions. I fill templated needed, can anybody show me the way? And 2nd question: what if additionally to written above, function shuld return ubyte[] or char[] ? can compiler guess what return type need from code? something like: char[] cc = myFunc() ubyte[] bb = myFunc() Thanks.
Jan 10 2016
On Sunday, 10 January 2016 at 10:10:46 UTC, zabruk70 wrote:Hello. 1st Novice question: i want function, operates sometimes with char[], sometimes with ubyte[]. internally it works with ubyte. i can use overloading: void myFunc(ubyte[] arg) {...}; void myFunc(char[] arg) { ubyte[] arg2 = cast(ubyte[]) arg; ...} It is OK. But i want 2 params (arg1, arg2), so i need write 4 overloading functions. I fill templated needed, can anybody show me the way?No you actually don't have to write all possible overloads. For example: (surely not the best solution) import std.traits; ubyte[] myFuncImpl(ubyte[] a, ubyte[] b) { return a ~ b; } ubyte[] myFunc(T1,T2)(T1[] a, T2[] b) if(is(Unqual!T1 == ubyte) || is(Unqual!T1 == char) || is(Unqual!T2 == ubyte) || is(Unqual!T2 == char)) { return myFuncImpl(cast(ubyte[]) a, cast(ubyte[]) b); }And 2nd question: what if additionally to written above, function shuld return ubyte[] or char[] ? can compiler guess what return type need from code?No. Your function call signature would be completely the same.. The way to do this is to make the function a template and pass the return parameter to it. So you're able to do something like: auto val = myReturnFunc!int(); togrue
Jan 10 2016
On Sunday, 10 January 2016 at 11:13:00 UTC, Tobi G. wrote:ubyte[] myFunc(T1,T2)(T1[] a, T2[] b)Tobi, big thanks!!! I should learn templates...
Jan 10 2016
On Sunday, 10 January 2016 at 10:10:46 UTC, zabruk70 wrote:void myFunc(char[] arg) { ubyte[] arg2 = cast(ubyte[]) arg; ...}void myFunc(const(void)[] arg) { const(ubyte)[] arg2 = cast(const(ubyte)[]) arg; // use arg2 } A `const(void)[]` type can accept any array as input. void[] is any array, const means it will take immutable, const, and mutable as well. Then you cast it to one type to use it.what if additionally to written above, function shuld return ubyte[] or char[] ?That depends on what you're doing, but you might also just want to return const(void)[], which the user will have to cast to something for them to use.
Jan 10 2016
On Sunday, 10 January 2016 at 14:17:28 UTC, Adam D. Ruppe wrote:A `const(void)[]` type can accept any array as input. void[] isAh, how i can forget about void[] ! Thanks Adam!
Jan 10 2016