Thread overview
Can't add a const ubyte to a dynamic array of ubyte?
Aug 20, 2019
ads
Aug 20, 2019
a11e99z
Aug 20, 2019
Daniel Kozak
Aug 20, 2019
a11e99z
Aug 20, 2019
Jonathan M Davis
August 20, 2019
import std.stdio;

ubyte[] extend(in uint[] arr)
{
	ubyte[] result;
	foreach (n; arr)
	{
		if (n < 10)
		{
			result ~= n;
                        // source/app.d(10,11): Error: cannot append type const(uint) to type ubyte[]
		}
		else
		{
			import std.conv : to;

			foreach (digit; n.to!string)
			{
				result ~= digit.to!ubyte;
			}
		}
	}
	return result;
}

unittest
{
	import std.algorithm : equal;

	assert(extend([1, 25, 70, 0]).equal([1, 2, 5, 7, 0, 0]));
}


How can I get around this? I want to ensure that the array is not mutated in the function in the signature too.

August 20, 2019
On Tuesday, 20 August 2019 at 09:27:36 UTC, ads wrote:
> import std.stdio;
>
> ubyte[] extend(in uint[] arr)
> {
> 	ubyte[] result;
> 	foreach (n; arr)
> 	{
> 		if (n < 10)
> 			result ~= n;
> 		else
> 		{
> 			import std.conv : to;
> 			foreach (digit; n.to!string)
> 				result ~= digit.to!ubyte;
> 		}
> 	}
> 	return result;
> }
>
> unittest
> {
> 	import std.algorithm : equal;
> 	assert(extend([1, 25, 70, 0]).equal([1, 2, 5, 7, 0, 0]));
> }
>
> How can I get around this? I want to ensure that the array is not mutated in the function in the signature too.

what u using here?
> result ~= digit.to!ubyte;
why not to do same in line?
> result ~= n;

2) digit is '0'+(0..9) so u need subtract '0' ('0' is \x30)
August 20, 2019
On Tue, Aug 20, 2019 at 11:30 AM ads via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote:
>
>
> How can I get around this? I want to ensure that the array is not mutated in the function in the signature too.
>

https://run.dlang.io/is/tehp3j

import std.stdio;

ubyte[] extend(in uint[] arr)
{
    ubyte[] result;
    foreach (n; arr)
    {
        if (n < 10)
        {
            result ~= cast(ubyte)n;
            // source/app.d(10,11): Error: cannot append type
const(uint) to type ubyte[]
        }
        else
        {
            import std.conv : to;

            foreach (digit; n.to!string)
            {
                result ~= cast(ubyte)(digit - '0');
            }
        }
    }
    return result;
}

unittest
{
    import std.algorithm : equal;
    assert(extend([1, 25, 70, 0]).equal([1, 2, 5, 7, 0, 0]));
}
August 20, 2019
On Tuesday, August 20, 2019 3:27:36 AM MDT ads via Digitalmars-d-learn wrote:
> import std.stdio;
>
> ubyte[] extend(in uint[] arr)
> {
>   ubyte[] result;
>   foreach (n; arr)
>   {
>       if (n < 10)
>       {
>           result ~= n;
>                          // source/app.d(10,11): Error: cannot
> append type const(uint) to type ubyte[]
>       }
>       else
>       {
>           import std.conv : to;
>
>           foreach (digit; n.to!string)
>           {
>               result ~= digit.to!ubyte;
>           }
>       }
>   }
>   return result;
> }
>
> unittest
> {
>   import std.algorithm : equal;
>
>   assert(extend([1, 25, 70, 0]).equal([1, 2, 5, 7, 0, 0]));
> }
>
>
> How can I get around this? I want to ensure that the array is not mutated in the function in the signature too.

arr contains uints, not ubytes, so n is a uint, and you're trying to append a uint to result, which is an array of ubytes. If you want to append n to result, then you need to convert it to a ubyte - either by casting or by using to!uint (the difference being that to will throw a ConvException if the value of n doesn't fit in a ubyte). D does not allow implicit narrowing conversions (because there's no guarantee that the value will fit in the target type), so you can't assign a uint to a ubyte without an explicit conversion - and that includes appending to an array of ubytes.

- Jonathan M Davis



August 20, 2019
On Tuesday, 20 August 2019 at 09:49:21 UTC, Daniel Kozak wrote:
> On Tue, Aug 20, 2019 at 11:30 AM ads via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote:
>>

you do not allow a person to think about a problem (and it’s easy here).
you carried him through a puddle now, but when he dives into Sea D, you will not be there :)