Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If <code>dim</code> returns an ArrayBuffer, then, no, you can't just use <code>println(multiarray.dim)</code>. Here are some simple alternatives though:</p> <ol> <li><p><strong>Define a method to return the dimensions as a String (in your multiarray class):</strong></p> <pre><code>def dimStr = dim mkString "x" </code></pre></li> <li><p><strong>Define a utility method for printing:</strong></p> <pre><code>def printdim(buffer: ArrayBuffer[Int]) = println(buffer mkString "x") // or def printdim(multiarray: MultiArray) = println(multiarray.dim mkString "x") </code></pre></li> </ol> <p><strong>Generalizing toString:</strong></p> <p>With a bit more effort, you can define a version of <code>println</code> that will work the way you'd like. The idea is to abstract the String-creation logic. The power of this approach is that, once the basic infrastructure is in place, you're able to control the "stringification" of any type just by bringing an appropriate implicit into scope.</p> <p>Start by defining a type class:</p> <pre><code>trait Stringable[T] extends (T =&gt; String) </code></pre> <p>Provide a default implementation of the type class (which just falls back on the ordinary <code>toString</code> method):</p> <pre><code>class DefaultStringable[T] extends Stringable[T] { def apply(x: T) = x.toString } object DefaultStringable { implicit def any2stringable[T]: Stringable[T] = new DefaultStringable[T] } </code></pre> <p>Implement the type class for your use case:</p> <pre><code>implicit object DimIsStringable extends Stringable[ArrayBuffer[Int]] { def apply(buf: ArrayBuffer[Int]) = buf mkString "x" } </code></pre> <p>Define a print method that uses the type class:</p> <pre><code>def myprintln[T](x: T)(implicit e: Stringable[T]) = e(x) </code></pre> <p>Fun.</p> <pre><code>import DefaultStringable._ myprintln(ArrayBuffer(1,2,3)) // prints "ArrayBuffer(1, 2, 3)" </code></pre> <p>Profit! </p> <pre><code>import DimIsStringable._ myprintln(ArrayBuffer(1,2,3)) // prints "1x2x3" </code></pre> <p>If desired, you can make the default type class always available without requiring any import. To do so, you provide the conversion in a companion module to the Stringable trait (as demonstrated below). The disadvantage of doing so is that you'll no longer have a compiler error to alert you if you forget to import an appropriate Stringable.</p> <pre><code>object Stringable { implicit def any2stringable[T]: Stringable[T] = new DefaultStringable[T] } </code></pre>
    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