Scala Array Generics vs Vector Generics - arrays

So I have been working on a refactor in my project to convert Vector's in my code to Array's. The reason being that my application needs to be very performant, and using while-iterations on Array's is significantly faster than for comprehensions and iterations on Vector's. (see this blog post for details)
However I have run into an issue I can't seem to easily find an answer for. I have tweaked my code to hide implementation details, and just boil down to the code needed to highlight the issue.
The following class structure, when using Vector, compiles totally fine:
sealed abstract class BaseClass {
def element: Int
}
sealed abstract class TypeA extends BaseClass {
def element = 2
def children: Vector[BaseClass]
}
case class TypeB(element: Int = 2) extends BaseClass
case class TypeAA(children: Vector[TypeA]) extends TypeA
case class TypeAB(children: Vector[TypeB]) extends TypeA
Now, when switching from using Vector to using Array, it no longer compiles:
sealed abstract class BaseClass {
def element: Int
}
sealed abstract class TypeA extends BaseClass {
def element = 2
def children: Array[BaseClass]
}
case class TypeB(element: Int = 2) extends BaseClass
case class TypeAA(children: Array[TypeA]) extends TypeA
case class TypeAB(children: Array[TypeB]) extends TypeA
I get the error: overriding method children in class TypeA of type => Array[BaseClass]; value children has incompatible type for both TypeAA and TypeAB classes.
I have a feeling I need to do an implicit conversion somewhere, but I am relatively new to Scala (only a couple months) and am not sure exactly how to use it.
Sorry if this has been asked elsewhere, I had trouble finding the correct search terms for the issue i am having.

I think you need to use _ <: BaseClass instead of using the generic type itself:
sealed abstract class TypeA extends BaseClass {
def element = 2
def children: Array[_ <: BaseClass]
}
This happens because the generic type parameter in Array is invariant, while in Vector it is covariant:
final class Array[T]
final class Vector[+A]
Hope that helps you.

This comes from the fact Vector[+A] is covariant in its type parameter A while Array[A] is not, meaning invariant in A
You can work around that fact using F-Bounded Polymorphism:
sealed abstract class TypeA[A :< Type[A]] extends BaseClass {
def children: Array[A]
}
case class TypeAA(children: Array[TypeA]) extends TypeA[TypeA]
case class TypeAB(children: Array[TypeB]) extends TypeA[TypeB]

Related

mypy TypeVar include bound class as well as subclasses

class Super:
#classmethod
def instantiate(cls) -> What goes here?:
return cls()
class Sub(Super):
pass
class Sub2(Super):
pass
When writing typehints, what's the canonical way to say a method should return an instance of the super class or any of its subclasses. Closest I could find is TypeVar("Super", bound="Super") but this still raises an error for the Super class in mypy
here you can use typing.Type to type-hint cls like this:
from typing import TypeVar, Type
C = TypeVar("C")
class Super:
#classmethod
def instantiate(cls: Type[C]) -> C:
return cls()
class Sub(Super):
pass
class Sub2(Super):
pass
reveal_type(Sub.instantiate()) # note: Revealed type is 'tmp.Sub*'
reveal_type(Sub2.instantiate()) # note: Revealed type is 'tmp.Sub2*'

Base Class vs Extended Classes

I'm a new learner, I'm practicing the base and child classes. My question is how do we decide which class should be instantiated, extended or the Baseclass?
Thanks in advance
package MavenProject2Package2;
import org.testng.annotations.Test;
import MavenProject2Package.JavaTesting;
public class JavaTesting2 extends JavaTesting
{
#Test
public void f1()
{
JavaTesting a1 = new JavaTesting();
System.out.println(a1.msg);
JavaTesting2 a2 = new JavaTesting2();
System.out.println(a2.msg);
}
}
Base class - it's a class which you should be extending from. - eg - superclass.
In superclass you may put some general fields and methods, which are used across your web app. For example, locators for header as well as footer items, because they are the same for all the pages (mostly).

how to get range class of objectProperty by its domain class in owlapi?

In my project, I'd like to get all the range class related to the given class by an restricted(somevaluefrom or allvalues from) objectproperties. I can get the restricted subclassofAxioms expressions including the given class, but how can I get the range class in these expressions? In other word, how can I get all the related classes to the given class excluding inherited subclass.
For example:
public static void printSubClassOfAxioms(OWLOntology ontology,OWLReasoner reasoner,OWLClass owlClass){
for(OWLSubClassOfAxiom ax:ontology.getSubClassAxiomsForSubClass(owlClass)){
OWLClassExpression expression=ax.getSuperClass();
System.out.println(ax);
System.out.println(expression);
}
}
The results are:
SubClassOf(<#FourCheesesTopping> <#CheeseTopping>)
SubClassOf(<#FourCheesesTopping> ObjectSomeValuesFrom(<#hasSpiciness> <#Mild>))
SubClassOf(<#FourCheesesTopping> ObjectAllValuesFrom(<#hasCountryOfOrigin> #Country>))
How can I just get the range classes #Country and #Mild
Thank you for your attention!
Write an OWLObjectVisitor and override the visit(OWL... Type) for the restrictions you're interested in. At that point,
type.getFiller()
will yield the class you're after.
Examples are in the documentation: https://github.com/owlcs/owlapi/wiki/Documentation
public class RestrictionVisitor extends OWLClassExpressionVisitor {
#Override
public void visit(#Nonnull OWLObjectSomeValuesFrom ce) {
// This method gets called when a class expression is an existential
// (someValuesFrom) restriction and it asks us to visit it
}
}

Incompatible static properties in three.d.ts, with latest TypeScript

I'm compiling three.d.ts (available from here) with the TypeScript develop branch. I get the following error:
Types of static property 'Utils' of class 'THREE.Shape' and class 'THREE.Path'
are incompatible
The problem is that
Shape defines a static Utils class
Shape indirectly inherits from Curve
Curve also defines a static Utils class with a signature unrelated to Shape.Utils
which is ill-formed according to the language spec. Summarised, three.d.ts contains something like the following code:
declare class A {
static Utils: {
f (): any;
}
}
declare class B extends A {
static Utils: {
// incompatible with A.Utils, without f(): any
g (): any;
}
}
Putting aside the question of why the type of a static member must be compatible with that of an inherited static member of the same name - which isn't the case in several other OO languages, but which does appear to be the case in TypeScript - I would like to know how to fix three.d.ts so I can compile it.
My current workaround is simply to copy and paste the signature of Curve.Utils into Shape.Utils, so that the latter structurally extends the former. But what is the "right" way to capture the signature of the underlying three.js file (here) in a .d.ts file? Is this a case of inheritance being used incorrectly?
Short answer is, per spec, Typescript doesn't allow member hiding through inheritance, like C# does automatically for example.
As defined in the language specifications section 8.2.3
A derived class inherits all members from its base class it doesn’t override. Inheritance means that a
derived class implicitly contains all non-overridden members of the base class. Both public and private
members are inherited, but only public members can be overridden. A member in a derived class is said
to override a member in a base class when the derived class member has the same name and kind
(instance or static) as the base class member. The type of an overriding member must be a subtype
(section 3.8.2) of the type of the overridden member, or otherwise a compile-time error occurs.
and
Base class static members can be overridden by derived class static members of any kind as >long as the
types are compatible, as described above.
Maybe they added some type checking into the latest compiler version which was missing before...
Here is a proposed solution, based on inheritance:
declare class UtilsA{
f():any;
}
declare class UtilsB extends UtilsA{
g():any;
}
declare class A {
static Utils:UtilsA;
}
declare class B extends A {
static Utils:UtilsB;
}

Using Collection sort for ArrayList

class UnfairContainer<T> implements Comparable<UnfairContainer>
{
private ArrayList<T> array = new ArrayList<T>();
public void sort()
{
Collections.sort(array);
}
public int compareTo(UnfairContainer o)
{
}
}
So i have my class that implements comparable but when i try to create the sort method that calls Collections.sort(), it gives me an error that says i can't call collection sort with an ArrayList . Can anyone help? and help me with my compareTo method, i'm stuck on how i'm suppose to compare each element within my ArrayList
The problem is that the list is not guaranteed to be sort-able. This is because with your current setup, T could be anything- including a class that does not implement Comparable and hence cannot be sorted by Collections. The type signature of Collections.sort() reflects this:
public static <T extends Comparable<? super T>> void sort(List<T> list);
To fix this, you need to put an upper bound on T to ensure that it is sort-able:
class UnfairContainer<T extends Comparable<T> >
implements Comparable<UnfairContainer<T> >
{
...
The T extends Comparable<T> means that T must be a class that implements Comparable. This lets Collections know that the ArrayList can be sorted, and everything works.
For more information, please refer to the java trail on bounded wildcards in generics

Resources