Note that there are some explanatory texts on larger screens.

plurals
  1. POScala: referencing companion object from a child class
    primarykey
    data
    text
    <p>I'm thinking of a following Scala class layout. I have a basic trait that represents an Item - an interface of what ought to be an immutable object that we can query for name, weight and do some object-specific stuff by invoking methods like <code>equip</code>:</p> <pre><code>trait Item { def name: String def weight: Int def equip // ... more abstract methods } </code></pre> <p>I can create <code>Item</code> implementations, creating case classes by hand, but I'd like to have some sort of "dictionary"-based items - i.e. a static map holds mapping from type ID to values, <code>name</code> and <code>weight</code> methods just query the dictionary with a stored type ID:</p> <pre><code>object Weapon { final val NameDict = Map(1 -&gt; "short sword", 2 -&gt; "long sword") final val WeightDict = Map(1 -&gt; 15, 2 -&gt; 30) } case class Weapon(typ: Int) extends Item { import Weapon._ def name = NameDict(typ) def weight = WeightDict(typ) def equip = ... // implementation for weapon } </code></pre> <p>So far, so good. I can use <code>Weapon(1)</code> to reference an item object for "short sword". However, the same basic principle applies to any other item types, such as <code>Armor</code>, which uses exactly the same <code>name</code> and <code>weight</code> implementation, but completely different <code>equip</code> and other abstract methods implementation, for example:</p> <pre><code>object Armor { final val NameDict = Map(3 -&gt; "bronze armor", 4 -&gt; "iron armor") final val WeightDict = Map(3 -&gt; 100, 4 -&gt; 200) } case class Armor(typ: Int) extends Item { import Armor._ def name = NameDict(typ) def weight = WeightDict(typ) def equip = ... // implementation for armor } </code></pre> <p>Looks pretty similar to <code>Weapon</code>, isn't it? I'd like to factor out the common pattern (i.e. implementation of lookups in companion object dictionaries and common <code>typ</code> value) in something like that:</p> <pre><code>abstract class DictionaryItem(typ: Int) extends Item { def name = ???.NameDict(typ) def weight = ???.WeightDict(typ) } object Weapon { final val NameDict = Map(1 -&gt; "sword", 2 -&gt; "bow") final val WeightDict = Map(1 -&gt; 15, 2 -&gt; 30) } case class Weapon(typ: Int) extends DictionaryItem(typ) { def equip = ... // implementation for weapon } </code></pre> <p>But how do I reference a children's companion object (like <code>Weapon.NameDict</code>) from parent class - i.e. what should I use instead of <code>???.NameDict(typ)</code> and how do I explain to the compiler that children's companion object must include these dictionaries? Is there a better, more Scala-esque approach to such a problem?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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