Skip to main content

Switch for object pattern matching

3 replies [Last post]
afreije
Offline
Joined: 2004-10-14

To replace if/instanceof/casting:

<br />
import javax.xml.transform.Source;<br />
...</p>
<p>public void handleSource(Source s) {<br />
  switch (s) {<br />
    case (SAXSource s1) {<br />
      ...<br />
    }<br />
    case (DOMSource s2) {<br />
      ...<br />
    }<br />
    case (StreamSource s3) {<br />
      ...<br />
    }<br />
  }<br />
}<br />

The semantics should be similar to catching of thrown exceptions.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ulfzibis
Offline
Joined: 2005-02-18
kcpeppe
Offline
Joined: 2003-06-15

This is not a bug and to vote for this "bug" in the bug database is really an abuse of that system. For one thing there is no way that one can vote against.

Now to this "bug". Switching on strings is really an abuse of string in this example. Just as goto has no real place in a structured language, switch has no real place in an OO language. Think about polymorphisum and the power that it brings to the table. Think about everything as a String an you're back into the world of procedural programming, something that we should have left behind quite some time ago.

forax
Offline
Joined: 2004-10-07

this package already do this :

http://www-igm.univ-mlv.fr/~forax/works/sprintabout/index.html

Rémi Forax