Monkey C - switch request

Hi,

can we have the switch statement added to the monkey C language?

TIA!
  • Former Member
    Former Member over 9 years ago
    Add my vote, switch would be great :)
  • Yes. There appears to be a bunch of supporting code in the compiler, but it isn't working.

    Travis
  • Agreed, switch would be great!

    What is the best way to deal with the absence of switch? I have some user-configurable fields in a data field I made and right now I'm using endless if -- else if -- else statements to determine which metrics are shown.. Any advice? Thanks!
  • Former Member
    Former Member over 8 years ago
    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.
  • +1 for a switch statement please
  • Former Member
    Former Member over 8 years ago
    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.
  • Former Member
    Former Member over 8 years ago
    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.


    Here's an example from my watch face, Orbit: https://apps.garmin.com/en-US/apps/ddeb875c-81b8-4b64-a277-53ba01465cd8
    Note! This code obviously won't compile and is stripped down to just show the concept.

    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]
  • I don't see that this is anything like a switch. A switch takes an integral value and does something for some of the possible values. An example of where you might use this would be in implementation of the callback passed to makeJsonRequest(). The code parameter has many legal values (all of the negative values here, as well as values 100-999 in the HTTP spec).

    In a simple case like this, you can use a simple dictionary to simulate the switch.

    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
    }
    }


    This idea can be extended to execute arbitrary code (not just return a string), with something like this.

    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();
    }
    }


    That still isn't as good as a switch because it doesn't do fall through, but it will work for many cases.

    Travis
  • Former Member
    Former Member over 8 years ago
    I don't see that this is anything like a switch


    I agree my code isn't anything like a traditional switch statement, but like you mentioned, neither is yours. I was just presenting a way I've organized my code to render out data pieces of data based on if they are enabled or not, etc. I think our approaches aren't that different. You've mapped an integer to a string. I've mapped my enum values to a class and called a function on it.
  • Former Member
    Former Member over 8 years ago
    I don't see that this is anything like a switch.


    I never claimed my example to be equivalent to a switch statement, was just presenting it as a way I've organized my code to avoid using if/else blocks, etc. in case it was helpful to anyone.