Scala how to pass Object type to type parameter? -


i've defined class this:

class counta [t : typetag] { --cutted-- } 

and need @ runtime instantiate class correct type. have function create correct type of object @ runtime:

  def convertsparktypestoscala(datatype: datatype) = {     datatype match {       case integertype => int       case booleantype => boolean       case floattype => float       case doubletype => double       case longtype => long       case bytetype => byte       case shorttype => short     }   } 

now when try instantiate class this:

      val scalatype = convertsparktypestoscala(dt)       println(scalatype)       val counta = new counta[scalatype.type]       println(scalatype) 

i exception , first println prints:

object scala.int

then got exception:

java.lang.unsupportedoperationexception: schema type scalatype.type not supported

so doesn't behave same writing type in code. question is, want possible? if so, how can achieved?

perhaps can try along these lines. i'm guessing trying doesn't make lot of sense because xy problem.

import scala.reflect.runtime.universe._  def convertsparktypestoscala(datatype: datatype): typetag[_] = {   datatype match {     case integertype => typetag[int]     case booleantype => typetag[boolean]     case floattype => typetag[float]     case doubletype => typetag[double]     case longtype => typetag[long]     case bytetype => typetag[byte]     case shorttype => typetag[short]   } }  val dt: datatype = longtype val scalatag = convertsparktypestoscala(dt) println(scalatag) val counta = new counta()(scalatag) 

counta has existential type counta[_], meaning if counta has methods take t or produce t, methods pretty useless now, because compiler doesn't know t is.

what tried compute information @ runtime in convertsparktypestoscala method , use information @ compile time tell compiler type counta be. compile program before run, not possible information flow runtime compile time.

if want type of counta computed based on dt

  1. you need compute @ compile time (with implicits or macros or...) and
  2. the precise type dt needs known @ compile time; meaning type of dt has longtype.type or integertype.type, not datatype.

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -