dc.setColor(Colors…
class Vehicle
{
var type; // enumerated integer for type (1 = car, 2 = truck)
var weight; // weight in lbs, as an integer
}
var fordF150= new Vehicle();
fordF150.type= 2;
fordF150.weight = 5000;
// Vehicle bitmap:
// 0x00WWWWTT; WWWW = weight, TT = type
// Option 1
var fordF150 = 0;
fordF150 |= 2; // truck
fordF150 |= (5000 << 8);
// Option 2 (even better)
var fordF150 = 0x00138802;
// Later...
var vehicle = fordF150;
var type = vehicle & 0xff;
var weight = (vehicle & 0xffff00) >> 8;
Being an optimization geek, I thought to take the low hanging fruit and converted all my const to var, as suggested above. However, this increased my memory usage.
Based on the memory profiler - the particular class went from 252 (1823) to 288 (1859) - just changing const to var for about a dozen attributes.
So, maybe this is only true for some constants, or maybe it's no longer true.
const notPi = 3.14;
const radius = 5;
System.println("The area of the circle is not: " + notPi * radius ^ 2);
System.println("The area of the circle is not: " + 3.14 * 5 ^ 2);
using Toybox.WatchUi;
class ciqtestView extends WatchUi.SimpleDataField {
const x = 5;
function initialize() {
SimpleDataField.initialize();
label = "My Label";
System.println("x = " + x);
}
function compute(info) {
return 0.0;
}
}
using Toybox.WatchUi;
class ciqtestView extends WatchUi.SimpleDataField {
function initialize() {
SimpleDataField.initialize();
label = "My Label";
System.println("x = " + 5);
}
function compute(info) {
return 0.0;
}
}
Like Java, Monkey C compiles into byte code that is interpreted by a virtual machine. Also like Java, all objects are allocated on the heap, and the virtual machine cleans up memory (Java through garbage collection, Monkey C through reference counting). Unlike Java, Monkey C does not have primitive types—integers, floats, and chars are objects. This means primitives can have methods just like other objects.