Scala generic method - No ClassTag available for T

27,789

To instantiate an array in a generic context (instantiating an array of T where T is a type parameter), Scala needs to have information at runtime about T, in the form of an implicit value of type ClassTag[T]. Concretely, you need the caller of your method to (implicitly) pass this ClassTag value, which can conveniently be done using a context bound:

def foo[T:ClassTag](count: Int, value: T): Array[T] = Array.fill[T](count)(value)

For a (thorough) description of this situation, see this document:

https://docs.scala-lang.org/sips/scala-2-8-arrays.html

(To put it shortly, ClassTags are the reworked implementation of ClassManifests, so the rationale remains)

Share:
27,789
Chuck
Author by

Chuck

Chuck Ocheret, Founder and CEO of ReadyPosition, is a 25+ year veteran of software engineering in the finance, multimedia, and scientific industries. He has been brought into numerous organizations to bring about massive culture change around high bandwidth computing, agile infrastructure, and software development methodologies. Chuck has run technology, architecture, and development at tier one Hedge Funds and Investment Banks where he has held front-to-back responsibility for Algorithmic Trading, Program Trading, Index and Statistical Arbitrage, Automated Market Making, Order Management, Trade Warehouses, Research Platforms, and Client and Market Connectivity for some of the most successful and highest volume trading operations in the world. Chuck was the original implementor of the auction systems at WR Hambrecht+Co, including OpenIPO, which was the central process for the IPO's of companies like Peet's Coffee, Overstock.COM, and Google. Chuck was a key contributor to Morgan Stanley's A+ programming language, one of the first projects open sourced by a financial institution http://aplusdev.org. Chuck holds a B.E.S. in BioMedical Engineering and a Masters in Electrical Engineering from the Johns Hopkins University. In his spare time he plays guitar, writes computer graphics software, and maintains an active black belt in Chinese Internal Martial Arts.

Updated on July 19, 2022

Comments

  • Chuck
    Chuck almost 2 years

    I'm relatively new to Scala and am trying to define a generic object method. However, when I refer to the parameterized type within the method I am getting "No ClassTag available for T". Here is a contrived example that illustrates the problem.

    scala> def foo[T](count: Int, value: T): Array[T] = Array.fill[T](count)(value)
    <console>:7: error: No ClassTag available for T
           def foo[T](count: Int, value: T): Array[T] = Array.fill[T](count)(value)
                                                                            ^
    

    Thanks in advance for help in understanding what is wrong here and how to make this contrived example work.

  • Chuck
    Chuck almost 11 years
    Fascinating. With 'import scala.reflect.ClassTag' this works. Thanks.
  • Chuck
    Chuck almost 11 years
    People may find this instructive as well - docs.scala-lang.org/overviews/reflection/… - since ClassManifests are going away.
  • AlvaPan
    AlvaPan about 8 years
    If we do any comparison of two values of type T in the function body, we need an implicit orderer parameter in additional to the ClassTag annotation.
  • bbarker
    bbarker about 8 years
    I found this Java-oriented question helpful as well to understand why we don't need ClassTags for e.g. Lists, but Martin's SIP also hinted at this: stackoverflow.com/questions/1817524/generic-arrays-in-java