Is there a way to get the name of a MATLAB handle object as a string?
Overwrite disp method of your from handle derived class! If you mean the type then call class(obj)
Related
I was implementing custom list class MyList<T> in kotlin. In that, I wanted to add insertSorted function, which inserts a new element into the list in sorted order. For that, T must implement comparator. So the prototype of that function will be fun <C> insertSorted(ele: C) where C:T, C:Comparable<T> But this is giving me Type parameter cannot have any other bounds if it's bounded by another type parameter error. I am not understanding what this error is. Also, this question did not help me much.
PS: The type I am passing to that function is declared as class MyClass : Comparator<MyClass>. So the bound where C:T, C:Comparator<T> is valid I guess.
For the meaning of the error, see this question:
Why can't type parameter in Kotlin have any other bounds if it's bounded by another type parameter?
But if your custom list contains elements of type T and you want to compare them, then T should implement Comparable<T>.
So this should be all you need:
class MyList<T: Comparable<T>> {
fun insertSorted(ele: T) {
}
}
I need to pass the type of a class as a parameter because of type erasure.
class Abc<T : Any>(private val clazz: KClass<T>)
I can get it to work when T is something like String, but I'm having trouble creating the argument for clazz when the type is KClass<MutableList<Foo<*>>>.
I've tried doing mutableListOf<Foo<*>>(), but then I get KClass<MutableList<out Foo<*>>> instead of KClass<MutableList<Foo<*>>>.
How can I create the KClass instance that I need?
If you need to construct an Abc<MutableList<Foo<*>>> so its methods end up taking and returning MutableList<Foo<*>>, it's enough to cast it:
val abc = Abc(MutableList::class) as Abc<MutableList<Foo<*>>>
(you could cast the argument to KClass<MutableList<Foo<*>>> instead, but this makes no difference).
But as Tenfour04's comment says, there are no different KClass instances for MutableList<Foo<*>>, MutableList<String>, etc. so:
you can't expect actually different behavior for Abc<MutableList<Foo<*>>> and Abc<MutableList<AnythingElse>> except for the casts the compiler inserts;
by using type erasure in this way, you are giving up some type safety, and make possible ClassCastExceptions far from the original cast.
i am trying to understand when to use the "in" keyword in generics as opposed to the "out" keyword (which allows assigning to subclasses).
I am actually following this tutorial if it matters.
Lets say we have the following class defintiion:
class ParameterizedConsumer<in T> {
fun toString(value: T): String {
return value.toString()
}
}
How does this even compile since value is not guaranteed to be a String ? is this what the in keyword does ? it tells the class that there is a guarantee the type wont be any other subclass ? I am just not clear on the usecase for it, can you help ?
the tutorial says i will be able to call the following but i am lost as to what it has changed:
val parameterizedConsumer = ParameterizedConsumer<Number>()
val ref: ParameterizedConsumer<Double> = parameterizedConsumer
assertTrue(ref is ParameterizedConsumer<Double>)
UPDATE: I get it now. Out means you can downcast when producing. and "In" means you can downcast when assigning.
So in java this is not allowed:
// Java
void demo(Source<String> strs) {
Source<Object> objects = strs; // !!! Not allowed in Java
// ...
}
but in kotlin we can fix that if we use the "out" keyword we can assign to a downcasted class (subclass). likewise with "in" we can pass in a subclass into the class internally to use but not outwardly.
it tells the class that there is a guarantee the type wont be any other subclass ? I am just not clear on the usecase for it, can you help ?
Say you have a function that wants to add some items to a list you supply. The items are of type Int.
Question: what kinds of list are acceptable to this function?
Answer: MutableList<Int>, MutableList<Number>, MutableList<Any>. Or, in short, MutableList<in Int>.
In the same spirit, let's explain the out projection.
Say you have a function that wants to get some elements from a list you supply. The items are of type Future.
Question: what kinds of list are acceptable to this function?
Answer: List<Future>, List<RunnableFuture>, List<ScheduledFuture>... or, in short, List<out Future>.
I'll answer part of your question
How does this even compile since value is not guaranteed to be a String
So what? You can call .toString() on any type. That's how you get a string you'll be returning.
I know there are various capabilities in Java with reflection.
For example:
Class<?> clazz = Class.forName("java.util.Date");
Object ins = clazz.newInstance();
I wonder if I could pass class dynamicaly in some method declaration in <> tags (or there is other way to do it if it must be fixed). I would like to change that class declaration dynamicaly; because I would like to write generic method for all types of classes.
In there, I have this:
List<Country>
Can I write it something diffrent with reflection? For example can it be somehow be achieved to pass class as parameter (or how else should be this done):
List<ins>
? I would appreciate examples.
This cannot be done because generics are a compile time feature. Once code is compiled, the only place where generics are exists are at method signatures, and they are only used for compiling new code.
When working with reflection, you are basicly working with raw types, and need to code according to that, that means, you can cast the returned result of newInstance() to the list type your need, for example:
List<Country> ins = (List<Country>)clazz.newInstance();
This is a safe operation to do, because you know at that point its empty, and isn't passed to any outside code.
I don't think this is possible. Generics in Java are implemented in a way that prohibits runtime access.
Generics are there so that the compiler can verify correct typing, but are no longer present at runtime (this is called "type erasure"). Reflection deals with the runtime representation of types only. As far as I know the only case where reflection has to deal with generics is to find out "fixed" type parameters of sub-classes, e.g. when you have class Bar<T> and class Foo extends Bar<String>, you can find out that the T of Bar is fixed to String in Foo using reflection. However, this is information found in the class file, too. Except that, reflection can only see or create raw-types.
I remember coming across some way to declare multiple Set handlers in a property but now I can't figure out how it's done. It's useful in that one can assign different data types and the Set handler does the conversion, but I get the error
'Set' is already declared
thoughts anyone?
It's not possible
It would be nice to be able to write both
sQuantity = "1234"
and
sQuantity = 1234
with two setter functions, but trying to write even one setter function with the wrong parameter type seems doomed to failure:-
error BC31064: 'Set' parameter must have the same type as the containing property.
If Visual Basic doesn't allow conversion between setter parameter type and property type then there is no way it would be possible to have two setter functions. If setter functions are forced to have the same type as the property, then it could not know which to run if there were more than one!
So I'd argue 'not only does it not seem possible, but it is actually not possible!'
There is a workaround
What you can do however, is have two properties of different types changing the same underlying variable, so that you can write
sQuantityFromString = "1234"
and
sQuantityFromInt = 1234
using
Public Shared WriteOnly Property sQuantityFromInt () As Integer
with a setter function that takes an integer as a parameter and with both properties setter functions modifying the same underlying string member variable.
Private Shared m_sQuantity As String = Nothing
As far as I know, you cannot have multiple Set statements for a class property. A property cannot be overridden.
You can use a setter functions (this is mostly a paradigm in Java) and overload that if you need to. Then I would also suggest making the property readonly.
One other option is to have the property be defined as an Object and in the set check the TypeOf of the value being used to set the property and do whatever business logic you want. The only problem with this approach is that then your property doesn't have type checking.