Hi,
can we have the switch statement added to the monkey C language?
TIA!
One way I've avoided if/else blocks when rending out my watch face data pieces is by creating a class for each piece of data that adheres to a common interface of methods, taking advantage of the "duck typing" nature of monkey-c. That way you can have one code path regardless of what you are trying to display and all the specifics for what is unique is encompassed in separate classes.
Could you give an example of this?
I'm another one who ends up chaining lots of if-then-else statements together. I'd love the learn a better way to do it.
class Complication {
var enabled;
function initialize (pc, sc, ac, theme) {
enabled = true;
}
function draw (dc, x, y) {
//! your common drawing code here
}
function isEnabled () {
return enabled;
}
function drawHighlight (dc, x, y, end) {
//! your common code here
}
} //! end Complication class
class Steps extends Complication {
function initialize (pc, sc, ac, theme) {
Complication.initialize(pc, sc, ac, theme);
if( Sys.getDeviceSettings().activityTrackingOn ) {
enabled = true;
} else {
enabled = false;
}
}
function draw (dc, x, y) {
Complication.draw( dc, x, y);
//! steps specific draw code here
}
}
class Date extends Complication {
function initialize (pc, sc, ac, theme) {
Complication.initialize(pc, sc, ac, theme);
}
function draw (dc, x, y) {
Complication.draw(dc, x, y);
//! Date specific draw code here
}
}
module OrbitComplicationsManager {
enum {
STEPS,
DATE,
BATTERY,
BLUETOOTH,
COMPLICATIONS_CNT
}
var complications = [
OrbitComplications.Steps,
OrbitComplications.Date,
OrbitComplications.Battery,
OrbitComplications.Bluetooth
];
function getComplication (compID) {
return complications[compID];
}
function getAllComplications (comps, theme, accent) {
var fg = //!
var bg = //!
var off = //!
for (var i = 0; i < COMPLICATIONS_CNT; i++) {
var theClass = getComplication(i);
comps= new theClass(fg, off, accent, theme);
}
}
function drawAll(dc, theme, accent) {
var comps = new [COMPLICATIONS_CNT];
getAllComplications(comps, theme, accent);
for (var i = 0; i < COMPLICATIONS_CNT; i++) {
if (comps.isEnabled()) {
comps.draw(dc, coords[0], coords[1]);
}
}
}
}
[/CODE]
var switch = {
501 => "Not Implemented",
500 => "Internal Server Error",
405 => "Method Not Allowed",
404 => "Not Found",
403 => "Forbidden",
401 => "Not Authorized",
400 => "Bad Request",
202 => "Accepted",
201 => "Created",
100 => "OK",
0 => "Unknown Error",
-1 => "Bluetooth Error",
-2 => "Bluetooth Host Timeout",
-3 => "Bluetooth Server Timeout",
-4 => "Bluetooth No Data",
-5 => "Bluetooth Request Cancelled",
-101 => "Bluetooth Queue Full",
-102 => "Bluetooth Request Too Large"
};
function onJsonResponse(code, data) {
var string = switch[ code ];
if (string != null) {
// do something with the string
}
}
function handle_501() {
}
function handle_500() {
}
// snipped
function handle_n102() {
}
var switch = {
501 => :handle_501,
500 => :handle_500,
// snipped
-102 => :handle_n102
};
function onJsonResponse(code, data) {
var symbol = switch[ code ];
if (symbol != null) {
var handler = self.method(symbol);
handler.invoke();
}
}
I don't see that this is anything like a switch
I don't see that this is anything like a switch.