title: Return generic value from switch expression in Java-12
body: <p>I am wondering if there is an option to return a generic type from a Java 12 switch expression.</p>
  
  <p>The basic code can look like that:</p>
  
  <pre><code>boolean result = switch(ternaryBool) {
      case TRUE -&gt; true;
      case FALSE -&gt; false;
      default -&gt; throw new IllegalArgumentException("Seriously?!");
  };
  </code></pre>
  
  <p>Is there anything against doing it this way?</p>
  
  <pre><code>T result = switch(ternaryBool) {
      case TRUE -&gt; BooleanUtils.toBoolean('true');
      case FALSE -&gt; new Integer(0);
      default -&gt; throw new IllegalArgumentException("Seriously?!");
  };
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>Maybe better example from my case: I need to have few classes which are representing primitive &amp; complex data structures. I have also factory method which is creating this <code>DataPointValue</code> based on an enum from other system and unknown value (<strong>let's forget about casting exceptions</strong>):</p>
  
  <pre><code>public static &lt;T&gt; IDataPointValue create(T value, DATA_TYPE dataType) throws Exception {
      try {
          switch (dataType) {
              case BOOL:
                  return new BoolDataPointValue((Boolean) value);
              case INT:
                  return new IntDataPointValue((Integer) value);
              case WORD:
                  return new IntDataPointValue((Integer) value);
              case STRING:
                  return new StringDataPointValue((String) value);
              case REAL:
              case FLOAT:
                  return new RealDataPointValue((Float) value);
              case DINT:
                  return new DIntDataPointValue((Integer) value);
              case DWORD:
                  return new DWordDataPointValue((Integer) value);
              default:
                  throw new Exception("Data Type not implemented: " + dataType);
          }
      } catch (ClassCastException e){
          throw new Exception("Could not create DPV in terms of Type incompability");
      }
  }
  </code></pre>
  
  <p>Is there any profit to move this code and use switch expression from Java 12?</p>