In Vala, some methods require an Array of uint8's (uint8[]) as a parameter. For example see http://valadoc.org/#!api=glib-2.0/GLib.FileStream.write
I have the following code, but no idea how to "cast" my value to an array:
...
uint8 some_integer = 7;
desc.write(???, 1);
...
In C I'd simply do:
...
uint8 some_integer = 7;
fwrite(&some_integer, 1, 1, desc);
...
but the Vala compiler is not amused about the &-operator. What to do?
You can create an array in Vala as int[] b = { 2, 4, 6, 8 };. Hence you should be able to create it with a single variable too as uint8 [] some_array = {some_integer};; in your case desc.write({some_integer}, 1);.
Here's a detailed guide on Vala for further references.
Related
I'm using C++ Builder Berlin Community edition.
I want to parse a string such as "1234' into a integer array such as N[1] = 1, N[2] = 2, ... .
I've tried using SubString(str, #, #), StrMove(str, #, #), Copy(str, #, #).
They all return "function not found". I've been developing code for over years now, so I know I am not including the correct header (either .h, or .hpp) file in my include statements, or the programmers at Embarcadero in their wisdom have modified C++ Builder to make these functions obsolete.
int Lock;
AnsiString N_Str;
randomize();
Lock = rand() * 1000;
N_Str = AnsiString(Lock);
L[0] = StrToInt(StrMove(N_Str, 1, 1));
L[1] = StrToInt(SubString(N_Str, 2, 1));
L[2] = StrToInt(copy(N_Str, 3, 1));
L[3] = StrToInt(copy(N_Str, 4, 1));
I was looking for codes to understand the difference between final and const in dart. After I found some code blocks and changed a bit, the outputs were surprising for me. How to explain these outputs? What causes this difference?
void main() {
final list1 = [1, 2];
final list2 = [1, 2];
print(list1);
print(list2);
print(list1 == list2); //false
const list3 = [1, 2];
const list4 = [1, 2];
print(list3);
print(list4);
print(list3 == list4); //true
}
const means the object is created at compile time instead of when the program is running. Dart does also guarantee that if you create two const objects with the same arguments, it will both points to the same compile time object. This optimization is possible since const objects MUST be immutable and we can therefore safely just share the same instance multiple times.
The == operator will by default (and such also on List) check if two objects are the same as in the same physical object in memory.
Since your two const created lists are created with the same objects, the two lists variables, list3 and list4, will end up pointing to the same exact object created by the compiler and therefore list3 == list4 is going to be true.
I am trying to understand the const keyword I found an article on GeeksforGeeks of const but I didn't understand it if we make the [1,2] constant then geek1 == geek2 will print true otherwise false how?
// Declaring a function
gfg() =>[1, 2]; // if we write here gfg() => const [1, 2]; then geek1 == geek2 will print true
// Main function
void main() {
// Assiging value
// through function
var geek1 = gfg();
var geek2 = gfg();
// Printing result
// true
print(geek1 == geek2);
print(geek1);
print(geek2);
}
== for arrays in dart compares references. Meaning that two arrays, although with identical contents can return false on comparison with ==.
const defines a compile-time constant, but it would also work with a class attribute. It's just important, that instead of creating a new array, the function returns the same reference twice.
The difference between the two functions is essentially, that in the first one, you define a new array with each call of the function (different reference each time), while in the second one you define a compile-time constant, that is returned on each call of the function (same reference each time).
So let's see some examples:
class Factory {
List<int> arr = [1, 2];
final finalArr = [1, 2];
static const constArr = [1, 2];
getValue() => [1, 2];
getInlineConst() => const [1, 2];
getRef() => arr;
getFinalRef() => finalArr;
getConstRef() => constArr;
}
void main() {
final f = new Factory();
print("values: ${f.getValue() == f.getValue()}");
print("const inline: ${f.getInlineConst() == f.getInlineConst()}");
print("reference: ${f.getRef() == f.getRef()}");
print("final reference: ${f.getFinalRef() == f.getFinalRef()}");
print("const reference: ${f.getConstRef() == f.getConstRef()}");
final refBefore = f.getRef();
f.arr = [1, 2];
final refAfter = f.getRef();
print("reference (with change inbetween): ${refBefore == refAfter}");
}
Output is:
values: false
const inline: true
reference: true
final reference: true
const reference: true
reference (with change inbetween): false
Dart canonicalizes constants. That means that const Foo(1) in one part of the program evaluates to the same object as const Foo(1) in another part of the program. There is only one object. That extends to constant lists too: const [1, 2, 3] will evaluate to the same constant list everywhere it occurs in the program.
Non-constant object creation creates a new object each time its evaluated: Foo(1) or new Foo(1) will create a new object each time. Same for list iterals.
So, identical(const [1, 2, 3], const [1, 2, 3]) is true, but identical([1, 2, 3], [1, 2, 3]) is false.
The == of Dart's built-in lists does not compare the contents of the list. It's inherited from Object and only checks whether it's the same object (basically using bool operator==(Object other) => identical(this, other);).
How do I convert the below java code to equivalent dart.
private static final byte[] mIdBytes = new byte[]{(byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x7E};
byte[] data;
System.arraycopy(mIdBytes, 2, data, 0, 4);
Is there any Dart method that does a similar kind of operation?
I was looking into this:
https://pub.dev/documentation/ckb_dart_sdk/latest/ckb-utils_number/arrayCopy.html
To match Java's System.arrayCopy(source, sourceOffset, target, targetOffset, length)
you should use
target.setRange(targetOffset, targetOffset + length, source, sourceOffset);
This is more efficient than using List.copyRange for some lists, for example copying between typed-data lists with the same element size (like two Uint8Lists).
Well, I found the way to do it.
you can just use
List.copyRange(data, 0, mIdBytes, 2);
This is a workaround I kinda found to be done in your case. This is called sublist(), this method will take the start index, and an end index.
IDEA:
Use sublist(), and copy the elements to be started from, that sourcePos = you_pos
Source array will be used like sourceArray.sublist(startIndext, endIndex)
The destination array will be initialized with the value using sublist()
Till what length the item should be added would be mentioned in the end index+2, since it will ignore the last item, and copy till the index-1
FINAL CODE
void main() {
List<int> source = [1, 2, 3, 4, 5, 6];
List<int> target = [];
int startPos = 1;
int length = 4;
// to ensure the length doesn't exceeds limit
// length+2 because, it targets on the end index, that is 4 in source list
// but the end result should be length+2 to contain a length of 5 items
if(length+1 <= source.length-1){
target = source.sublist(startPos, length+2);
print(target);
}else{
print('Cannot copy items till $length: index out of bound');
}
}
//OUTPUT
[2, 3, 4, 5, 6]
Same post as this one, but I need the answer for Dart language.
I have a list:
List(1,2,3,4,5,6)
that I would like to to convert to the following map:
Map(1->2,3->4,5->6)
Which is the best way to implement this in dart?
You could just use a for loop, note that there might be more straightforward ways
var list = [1,2,3,4,5,6];
var map = <int, int>{};
for(var i = 0; i < list.length; i+=2) {
map[list[i]] = list[i+1];
}
print(map); //{1: 2, 3: 4, 5: 6}
(Throws an error for odd lists)