Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This looks like a bug to me (at least in 2.8.0 Beta1 where I've tested it).</p> <p>Particularly instructive is the following:</p> <pre><code>scala&gt; var x: SampleEnum.type#Value = null x: SampleEnum.Value = null </code></pre> <p>Here, we're requesting the <em>arbitrary</em> inner type but we're actually getting the <em>specific</em> inner type. That's just broken (and I will file a bug report if there is not already one, unless someone else swiftly explains why this is not a bug).</p> <p>So, what to do? Well, first, let's understand the original method signature of parse:</p> <pre><code>def parse[T &lt;: Enumeration](name:String, enum:T):T#Value </code></pre> <p>We have <code>T</code> which is a subclass of <code>Enumeration</code>, <code>enum</code> which is an instance of <code>T</code>, and--since there is no way to express that the <code>Value</code> must be from <em>that particular instance</em> of <code>T</code>, we have to resort to <code>T#Value</code> (i.e. an inner type of <code>T</code>, without regard to which particular <code>T</code> it comes from).</p> <p>Now we have to pass in a specific object, get back a generic inner object, and do it in the face of <code>ExampleObject.type#Value</code> being the same as <code>ExampleObject.Value</code> even though they type differently.</p> <p>So we have to write our own object from scratch:</p> <pre><code>class SampleWorkaroundClass extends Enumeration { val include, exclude = Value } lazy val SampleWorkaround = new SampleWorkaroundClass </code></pre> <p>Here we have a single instantiation of a specially defined class. Now we can get around the bug in <code>Object</code>:</p> <pre><code>scala&gt; val typeWorks:SampleWorkaroundClass#Value = parse("include",SampleWorkaround) typeWorks: SampleWorkaroundClass#Value = include </code></pre> <p>(The <code>lazy val</code> is just to get exactly the same behavior as with objects where they're not instantiated until they're used; but a <code>val</code> alone would be fine.)</p> <hr> <p>Edit: <code>Outer#Inner</code> means "any inner class of type <code>Inner</code> coming from this outer class" as opposed to <code>myOuter.Inner</code> which means "only that class of type <code>Inner</code> that has this instance of <code>Outer</code>, <code>myOuter</code>, as its enclosing class". Also, I don't get the dependent type error in 2.8.0 Beta1--but not being able to specify the type makes things quite awkward.</p> <hr> <p>Edit: update on bug report--the way it works now is apparently intentional. To use types in this fashion, you are intended to explicitly specify the type in the function call (as it is not inferred to be what you want), like so</p> <pre><code>val suggested: SampleEnum.type#Value = parse[SampleEnum.type]("include",SampleEnum) </code></pre> <p>This way is easier if you only have to do this a few times. If you have to do it many times, creating your own class with a val (or lazy val) instantiation probably makes things easier / more compact.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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