get classOf[Array[A]] from classOf[A] - arrays

I have a list of scala classes in the form like:
List(classOf[A], classOf[B], ...)
I need to register these classes as well as the array of the classes into kryo. The result is like:
kryo.register(classOf[A])
kryo.register(classOf[Array[A]])
kryo.register(classOf[B])
kryo.register(classOf[Array[B]])
...
So, with the list at hand, I may just use a foreach to register both the class and the array of the class.
However, I fail to get classOf[Array[A]] from classOf[A]. I have tried the ClassTag as following method:
def getArrayClass[T: ClassTag](c: Class[T]): Class[_] = {
classOf[Array[T]]
}
The result is not the right (though the type of the both results is the same), and kryo still complains that Class is not registered: A[].
scala> getArrayClass(classOf[A])
res0: Class[Array[A]] = class java.lang.Object
scala> classOf[Array[A]]
res1: Class[Array[A]] = class [LA;
Any clues? Thanks.

You can do it using the wrap method ClassTag:
def getArrayClass(c: Class[_]): Class[_] =
scala.reflect.ClassTag(c).wrap.runtimeClass
Note that this does not work if c is classOf[Null] or classOf[Nothing] (I believe this is bug in ClassTags). For any other Class, it will work.
You can also go to the Java way of doing it, without ClassTags, which is basically equivalent:
def getArrayClass(c: Class[_]): Class[_] =
java.lang.reflect.Array.newInstance(c, 0).getClass

Related

Getting unexpected error when using structure type

I'm new to Scala programming, so please bear with me if it's a silly question. I was expecting the following code to work, since a has a mkString method.
val a = "abc".toCharArray
case class A[T: {def mkString(): String}](i: T) {
val s = i.mkString
}
A(a)
But it gives the following error:
Error:(3, 16) AnyRef{def mkString(): String} does not take type
parameters case class A[T: {def mkString(): String}](i: T)
Error:(4, 14) value mkString is not a member of type parameter T val
s = i.mkString
I'm using Scala 2.11.6.
Any help will be really appreciated! Thank you !
The type bounds [A : B] is a shorthand way of saying there exists an instance of the typeclass B for type A, so these are equivalent:
def foo[A : B]: C
def foo[A](implicit b: B[A]): C
The syntax you're probably looking for is [A <: B], which means A must be a subclass of B.
But then you have the issue that mkString isn't actually in the Array class; it's defined in ArrayOps (with an implicit conversion from Array) as described in the documentation:
This member is added by an implicit conversion from Array[T] to
ArrayOps[T] performed by method genericArrayOps in scala.Predef.
You can work around that with:
val a = "abc".toCharArray
case class A[T](i: T)(implicit conv: T => {def mkString: String}) {
val s = conv(i).mkString
}
A(a)
At least three mistakes there:
It should be <:, not :. The structural type is not a typeclass.
def mkString(): String is not the same as def mkString: String.
raw JVM arrays know nothing about convenient scala methods like mkString. You need a WrappedArray for that.
This here works:
import scala.language.reflectiveCalls
val a = "abc".toCharArray
import collection.mutable.WrappedArray
case class A[T <: { def mkString: String }](i: T){
val s = i.mkString
}
A(a: WrappedArray[Char])

Maya Python static class function scope

I have some trouble getting my static class to work. There is something I am missing about the scope of functions within a class. If called the script gives me following error:
NameError: global name 'disableCostumFrames' is not defined #
import maya.cmds as cmds
from functools import partial
class Blast:
def createWindow():
# Todo:
# hanldes the gui for the user
windowID = 'window'
if cmds.window(windowID, exists = True):
cmds.deleteUI('window')
window = cmds.window(windowID, title="Blast", iconName='Blast', widthHeight=(400, 200) )
cmds.frameLayout( label='')
cmds.rowColumnLayout( numberOfColumns=4, columnWidth=[(1, 100),(3, 100)] )
cmds.text( label='Start: ' )
global Blast_startFrame
Blast_startFrame = cmds.textField( enable = False)
cmds.text( label=' End: ' )
global Blast_endFrame
Blast_endFrame = cmds.textField( enable = False)
cmds.setParent('..')
cmds.rowColumnLayout( numberOfColumns=2, columnWidth=[(1, 100), (2, 100)] )
cmds.radioCollection()
#cmds.radioButton( label='Full', select = True, onCommand= partial(disableCostumFrames, Blast_startFrame, Blast_endFrame ) )
#cmds.radioButton( label='Costum', onCommand= partial(enableCostumFrames, Blast_startFrame, Blast_endFrame ) )
cmds.setParent('..')
cmds.rowColumnLayout( numberOfColumns=1, columnWidth=[(1, 400), (2, 100)] )
cmds.button( label='Playblast' ,command= 'createPlayblast')
cmds.setParent('..')
cmds.showWindow( window )
return Blast_startFrame, Blast_endFrame
def main():
createWindow()
def enableCostumFrames(Blast_startFrame, Blast_endFrame, *args):
cmds.textField(Blast_startFrame, edit=True, enable=True)
cmds.textField(Blast_endFrame, edit=True, enable=True)
def disableCostumFrames(Blast_startFrame, Blast_endFrame, *args):
cmds.textField(Blast_startFrame, edit=True, text="", enable=False)
cmds.textField(Blast_endFrame, edit=True, text="", enable=False)
How do I need to define these functions within the class? I am calling the module like that:
import sys
Dir = 'c:/Blast'
if Dir not in sys.path:
sys.path.append(Dir)
try: reload(Blast_v011)
except: import Blast_v011
Blast_v011.Blast()
maybe iam doing something wrong on this side? Appreciated any help.
In this case you need to add a self reference to all of the methods in your class. The usual python class looks like this:
class MyClass(object):
def __init__(self):
self.variable = 123
def some_method(self):
print "my variable = ", self.variable
def some_other_method(self):
if self.variable > 1:
self.some_method()
The self reference in the member functions is how you get at class member variables and other functions -- it's python's way of referring to what other languages call this.
Instance methods can only be called on an instance (it's the instance that gets passed in as self). You can make a method that is called on the class itself -- rather than any particular instance of the class -- using the #classmethod decorator. Classmethods also take an argument, but instead of self it's a reference to the class. You use it the same way to get variables defined at the class level, which are shared by all copies of the class:
class HasShared(object):
shared = 99
#classmethod
def a_class_method(cls):
print cls.shared
(You can mix and match class and instance methods in the same class).
You can also make static methods using the #staticmethod decorator. These don't get a default argument at all:
class NotPythonic(object):
#staticmethod
def needs_no_args():
print "call me as NotPythonic.needs_no_args()"
In Python we tend to avoid this formula, since you can get a static method by just creating a function in a module without making a class to hold them. For the example code you posted, I'd probably just make a conventional class using instance methods, since your functions need the names of the gui widgets to be able to actually ask them questions.

Scala type inference: can't infer IndexedSeq[T] from Array[T]

In Scala 2.11.2, the following minimal example compiles only when using type ascription on the Array[String]:
object Foo {
def fromList(list: List[String]): Foo = new Foo(list.toArray : Array[String])
}
class Foo(source: IndexedSeq[String])
If I remove the type ascription in fromList, it will fail to compile with the following error:
Error:(48, 56) polymorphic expression cannot be instantiated to expected type;
found : [B >: String]Array[B]
required: IndexedSeq[String]
def fromList(list: List[String]): Foo = new Foo(list.toArray)
^
Why can't the compiler infer the Array[String] here? Or does this issue have to do something with the implicit conversion from Array's to IndexedSeq's?
The issue is that the .toArray method returns an Array of some type B which is a superclass of the T in List[T]. This allows you to use a list.toArray on List[Bar] where an Array[Foo] is required if Bar extends Foo.
Yes, the real reason this doesn't work out of the box is that the compiler is trying to figure out which B to use and also how to get to an IndexedSeq. It seems like it's trying to resolve the IndexedSeq[String] requirement but B is only guaranteed to be a String or a superclass of String; hence the error.
This is my preferred work around:
def fromList(list: List[String]): Foo = new Foo(list.toArray[String])

How to iterate over a list of type Class to edit the properties of its objects in Groovy

I know there are more elaborate ways to achieve this in Java, but Groovy should have a concise way to do the same as per http://groovy.codehaus.org/Looping
Class Currency.groovy
class Currency {
String name
double rate
}
CurrencyController
def select(){
List<Currency> selectedCurrencies = Currency.getAll(params.currencies)
selectedCurrencies.eachWithIndex { obj, i -> obj.rate = update(obj.name)};
[selectedCurrencies:selectedCurrencies]
}
def update(String sym){
return sym
}
The above code throws:
No signature of method: currencychecker.CurrencyController$_$tt__select_closure12.doCall() is applicable for argument types: (currencychecker.Currency)
Thanks to #dmahapatro, the issue was that I was using an iterator variable obj[i], even though obj itself is the iterated object. The rest is correct!
I experimented with selectCurrencies.each as well instead of selectCurrencies.eachWithIndex however the right one in this case is eachWithIndex

Tuple case mapping don't work with generic Array[T] in scala

I don't understand why the compiler cannot understand the case instruction mapping on tuple when i try to use with generics Array[T].
class Variable[T](val p: Prototype[T], val value: T)
class Prototype[T](val name: String)(implicit m: Manifest[T])
// Columns to variable converter
implicit def columns2Variables[T](columns:Array[(String,Array[T])]): Iterable[Variable[Array[T]]] = {
columns.map{
case(name,value) =>
new Variable[Array[T]](new Prototype[Array[T]](name), value)
}.toIterable
}
Error say :
error: constructor cannot be instantiated to expected type;
found : (T1, T2)
required: fr.geocite.simExplorator.data.Variable[Array[T]]
case(name,value) =>
I'm also not sure about the wording of the error, but first of all, you will need the manifest for T because it is required for constructing new Prototype[Array[T]] (the array manifest can be automatically generated if a manifest for its type parameter is in scope).
Is there any reason you absolutely need arrays? They come with the irregularity of Java's type system, they are mutable, and they offer very little advantage over for example Vector. Lastly, and that's probably why carry around the manifests, unlike arrays standard collections do not require manifests for construction.
class Variable[T](val p: Prototype[T], val value: T)
class Prototype[T](val name: String)
implicit def cols2v[T](cols: Vector[(String,Vector[T])]): Vector[Variable[Vector[T]]] =
cols.map {
case (name, value) => new Variable(new Prototype(name), value)
}

Resources