Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

java switch

Name: Anonymous 2011-10-20 11:32

Why does java not allow String objects to be used in the switch/case statements? Couldn't the compile invoke hashcode() on those strings thus producing a unique integer value to represent the constant in the cases?

Name: Anonymous 2011-10-21 1:37

You could create an interface that has only one member function, which takes as arguments everything that the switch statement needs to know, and then make instances of anonymous classes that implement the interface, and then insert these code handlers as values in a hash table, where the keys are the strings.


static void final public package javaFunctionFNJavaHelper(String value, String blah) {
  value = value + blah;
  switch(value) {
    case "Java":
      System.out.println("merpa " + value);
      break;
    case "Is":
      System.out.println("derpi " + value);
      break;
    case "Fun":
      System.out.println("dapity " + value);
      break;
    case "Is it Not?":
      System.out.println("dooooo " + value);
      break;
    default:
      System.out.println("Go fuck yourself.");
      break;
  }
}


Can be implemented very compactly with:


Interface MyHandler
{
  public void exec(String value);
}

class MySwitchHandler implements MyHandler {
  public SwitchHandler() {
    this.table = new HashTable<String, MyHandler>();
    this.defaultCase = null;
  }

  public void exec(String value) {
    MyHandler handler = table.get(value);
    if (handler == null) {
      defaultHandler.exec(value);
    }
    else {
      handler.exec(value);
    }
  }

  public void putCase(String key, MyHandler value) {
    table.put(key, value);
  }

  public void putDefaultCase(MyHandler value) {
    defaultCase = value;
  }

  private HashTable<String, MyHandler> table;
  private MyHandler defaultCase;
}

javaFunctionFNJavaHelper(String value, String blah) {
  MySwitchHandler switchStatement = new MySwitchHandler();
  switchStatement.putCase("Java", new MyHandler() {
    public void exec(String value) {
      System.out.println("merpa " + value);
    }
  });
  switchStatement.putCase("Is", new MyHandler() {
    public void exec(String value) {
      System.out.println("derpi " + value);
    }
  });
  switchStatement.putCase("Fun", new MyHandler() {
    public void exec(String value) {
      System.out.println("dapity " + value);
    }
  });
  switchStatement.putCase("Is it Not?", new MyHandler() {
    public void exec(String value) {
      System.out.println("dooooo " + value);
    }
  });
  switchStatement.putDefaultCase(new MyHandler() {
    public void exec(String value) {
      System.out.println("Go fuck yourself");
    }
  });
  switchStatement.exec(value);
}


And any language that supports both easy syntax for hash tables and closures, you can do this:


function make_switch_statement(case_table, default_case)
  meta_table = {__index = function(tab, key)
    local value = rawget(tab, key)
    if value == nil then
      return default_case
    else
      return value
    end
  end}
  setmetatable(case_table, meta_table)
  return case_table
end

function woop(value)
  print(make_switch_statement({
    Lua = function()
      return 'merki '..value
    end,
    Is = function()
      return 'derpity '..value
    end,
    Fun = function()
      return 'dipity '..value
    end,
    ["Is it Not?"] = function()
      return 'dooooo '..value
    end,
  }, function()
    return 'Go have fun with yourself.'
  end)[value]())
end

woop('Is') -- prints derpity Is
woop('Waba') -- print Go have fun with yourself.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List