Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>(Not a complete solution, but might give some ideas)</p> <p>One impressive feature of Scala is its ability to return a list of Fruits when an orange is appended to a list of apples. It's fine with values, precisely because you let the generic type be inferred.</p> <pre><code>import scala.reflect.Manifest def CommonSuperType[A, B &gt;: A : Manifest](a:A, b:B) = manifest[B] </code></pre> <p>It works (kind of) :</p> <pre><code>scala&gt; CommonSuperType(new JButton, new JPanel) res42: Manifest[javax.swing.JComponent with javax.accessibility.Accessible] = javax.swing.JComponent with javax.accessibility.Accessible </code></pre> <p>Next step would be to lift this trick to higher kinded types (not tested).<br> An half baked solution consists in creating values from types (cf <a href="https://stackoverflow.com/questions/818996/abstract-types-type-parameters-in-scala">this answer</a>) :</p> <pre><code>class CommonSuper[A:Manifest, B:Manifest] { def make[T:Manifest] = manifest[T].erasure.newInstance.asInstanceOf[T] val instanceA = make[A] val instanceB = make[B] def getType = CommonSuperType(instanceA, instanceB) } </code></pre> <p>But I'm stuck in this unintuitive inconsistency :</p> <pre><code>scala&gt; val test = new CommonSuper[JButton, JPanel] scala&gt; test.getType res66: Manifest[Any] = Any scala&gt; CommonSuperType(test.instanceA, test.instanceB) res67: Manifest[javax.swing.JComponent with javax.accessibility.Accessible] = javax.swing.JComponent with javax.accessibility.Accessible </code></pre> <p>Anyway, whereas I'm fond of this type of questions (questions about types), here it smells like an <a href="https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem">XY Problem</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload