www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Can't add a const ubyte to a dynamic array of ubyte?

reply ads <relay.public.adnan outlook.com> writes:
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.
Aug 20 2019
next sibling parent a11e99z <black80 bk.ru> writes:
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)
Aug 20 2019
prev sibling next sibling parent reply Daniel Kozak <kozzi11 gmail.com> writes:
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])); }
Aug 20 2019
parent a11e99z <black80 bk.ru> writes:
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 :)
Aug 20 2019
prev sibling parent Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
Aug 20 2019