Function accepts const ubyte[], const char[], immutable ubyte[], immutable char[]

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jan 10 06:17:28 PST 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.



More information about the Digitalmars-d-learn mailing list