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-20 11:38

why don't you just switch(hashcode)?

Name: Anonymous 2011-10-20 11:45

>>2
Can't declare a static final int using string plus hashcode.

Ie:
final int a = "dick".hashcode();

Gives a compile error.

Name: Anonymous 2011-10-20 11:46

Name: Anonymous 2011-10-20 12:02

Name: Anonymous 2011-10-20 12:04

>>5
Java 7
U MENA GNU/HURD 12.2

Name: Anonymous 2011-10-20 12:09

>>6
I'm not sure what you're implying here. It was released some time ago, like a month or something.

Name: Anonymous 2011-10-20 12:32

>>7
like a month
Implying that it is somehow ready for production because of it's maturity. QA is calling and they want to talk to your boss... about you.
Enjoy your unemployment.

Name: Anonymous 2011-10-20 14:01

Why Java 7?

Scala is battar

Name: Anonymous 2011-10-20 14:06

>>9
ABCL > scala

Name: Anonymous 2011-10-20 14:07

The Tanasinn ate your hashcode.

Name: Anonymous 2011-10-20 14:09

So? C can't do it either? If something can't be done in C, then it shouldn't be done at all.

Name: Anonymous 2011-10-20 14:12

>>12

Object-oriented programming shouldn't be done

Name: Anonymous 2011-10-20 14:13

>>13
Correct.

Name: Anonymous 2011-10-20 14:18

>>1
When you hash a String in Java it doesn't always return unique values, two different strings might return the same value.

>>13
You can do object oriented programming in ANSI-C.

Name: Anonymous 2011-10-20 14:18

>>13
I agree with this poast.

Name: Anonymous 2011-10-20 14:19

the switch statement is considered harmful anyway. use the state pattern instead

Name: Anonymous 2011-10-20 15:32

>>17
I had to look up what the "state pattern" is, and got lost after the second sentence of the description.

Yay for inefficiency and overcomplexity.

Name: Anonymous 2011-10-20 15:33

>>18
if you are unable to understand simple design patterns maybe you should stop programming altogether

Name: Anonymous 2011-10-20 16:00

>>17
What's next, the pattern pattern?

Name: Anonymous 2011-10-20 16:11

>>20
I think that would be considered an anti-pattern.

Name: Anonymous 2011-10-20 16:22

Your floor is now Java

Name: Anonymous 2011-10-20 17:42

>>22
Quick! Stand on the CouchDB!

Name: Anonymous 2011-10-20 19:57

I did write a string switch macro In c years ago.  It was just a loop and if statements #defined to look like a switch . I'll dig out the code tomorrow

Name: Anonymous 2011-10-20 20:15

>>22
my floor is now clean thanks to GC
also, my floor is now ENTERPRISE QUALITY!!

Name: Anonymous 2011-10-20 22:22

>>17
Implying making every string of mind a new class that extends string would somehow be better than switch let alone if/else

Name: Anonymous 2011-10-20 22:25

>>24
C
strings
you mena array of chars, right ?

Sounds like you're just comparing each char to each other also and seeing if they match up.

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.

Name: Anonymous 2011-10-21 2:39

>>25
Cleaning floors is wasteful and timeconsuming.
Just buy some cockroaches and they'll automatically eat it up.
Plus, you get free pet food.

Name: Anonymous 2011-10-21 6:48

>>28
You might want to consider using the Runnable interface for your MyHandler so that you can integrate better with legacy code.

Name: Anonymous 2011-10-21 6:56

As promised here is my C string switch:


/*
strswitch.h
Written in 2004 by Anonymous.
ENTERPRISE-GRADE C
*/

#ifndef _STRSWITCH_H
#define _STRSWITCH_H

#include <stdlib.h>
#include <string.h>

void *strswitch_var;
int   strswitch_loop;
int   strswitch_enter;

int strswitch_match(char *s) {
  char *p;
  if(strswitch_enter)
    return 1;
  if(*(char**)strswitch_var) {
    if(*s=='"') {
      p = malloc(strlen(s)+1);
      if(!p)
        return 0;
      strcpy(p++,s);
      *strchr(p,'"') = 0;
      strswitch_enter = !strcmp(p,*(char**)strswitch_var);
      free(p-1);
    }
  }
  else
    strswitch_enter = !strncmp(s,"NULL",4);
  return strswitch_enter;
}

#define strswitch(var)  strswitch_var = &var;\
                        strswitch_enter = 0;\
                        for(strswitch_loop=1;strswitch_loop;strswitch_loop=0)

#define case(c)         if(strswitch_match(#c))

#define default()       strswitch_enter = 1;

#endif


Sample usage:


#include <stdio.h>
#include "strswitch.h"

int main(int argc, char **argv) {
  strswitch(argv[1]) {
    case(NULL) {
    }
    case("--help") {
      printf("usage:\n");
      break;
    }
    case("--version") {
      printf("%s 0.1\n",argv[0]);
      break;
    }
    default() {
      printf("non option\n");
      break;
    }
  }

  return 0;
}

Name: Anonymous 2011-10-21 7:04

GC is shit.

Name: Anonymous 2011-10-21 7:11

x=2; can you do this in Cee?
lolwut=function(){return 2}
switch(x){
case 1.01001: ;;break;
case "Sepples":;;break;
case 0:;;break;
case x:;window.title+='1';
case lolwut():;window.title+="2";break;
default:;;break;
}

Name: Anonymous 2011-10-21 8:38

>>28
HORRIBLE

Name: Anonymous 2011-10-21 8:42

>>31

nice, but I'd add __thread or something similar for thread safety

Name: Anonymous 2011-10-21 10:06

>>35
yeah i did know it wasn't thread safe,  but didn't know about the __thread ..  I had consider some kind of switch where you pass in the struct for the internal vars, but that wouldn't be so clean.  (Lol like anyone would ever use it)

Name: Anonymous 2011-10-21 10:39

java shit

Name: Anonymous 2011-10-21 13:43

>>17
Everything is considered harmful nowadays.

What do you think about multiple if/elses?

Name: Anonymous 2011-10-21 13:46

>>17
objectz r da bezt XD
No

Name: Anonymous 2011-10-21 13:58

>>38
Use the choice pattern!

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