Getting current class - reflection

how can I get the current class in Kotlin. For example if there are the abstract class "Animal" and the child-class "Dog" (witch is a child of "Animal") how can I get the class of "Dog" in "Animal". I don't now witch classes will use "Animal"
I already tried to use:
this::class
(what could be in Java):
this.getClass()
But this method is according to my IDE not available.
Best regards
Jonas Franz

Use javaClass to get a Class instance for this object. If you want to get a Class object for a particular class literal use Dog::class.java.

Related

Create object dynamically from string in Peoplecode

I work on a Peoplesoft project and I am struggling with Peoplecode.
I want to create an object with a dynamic classname within Peoplecode. Kind of like in Java. This would look like something like this :
&my_object = create My_Application_Package : Class_string_name()
&my_object.commonMethodCall();
where "Class_string_name" would be dynamic. Is this possible ?
And do I mandatoraly need to create an interface for all the concerned classes ?
Any help or advise is welcomed
Thanks
Finally, it is quite simple.
I just used CreateObject function :
CreateObject(str_class_name, create_par, . . .)
Where str_class_name either:
—identifies a class by class name
—identifies a class of OLE Automation object in the form:
app_name.object_name
Description
Use the CreateObject function to return an instance of a class. You
can use this function to access an Application Class, a PeopleCode
built-in object (like a chart), or an OLE Automation object.
If the class you are creating requires values to be passed, use the
create_par parameters to supply them, or use the CreateObjectArray
function.
Considerations Using Application Classes
You can use the CreateObject function to access an Application Class.
You would want to do this when you were programming at a high-level,
when you might not know the name of the class you wanted to access
until runtime. You must specify a fully-qualified class name. In
addition, the class name is case-sensitive.
The returned object has the type of class you specified.

Determine whether an instance is an instance of a data class

Given the following classes
abstract class SomeAbstractClass { abstract val name: String }
data class DataClass( override val name: String ) : SomeAbstractClass()
class NoDataClass( override val name: String ) : SomeAbstractClass()
For any instance of SomeAbstractClass, can I determine whether it is a data class without relying on type checking?
Some background: this seemed the best way of combining inheritance and data classes to me, as suggested in a different answer. Now, within the initializer block of SomeAbstractClass, I want to throw an exception in case the derived type is not a data class to ensure 'correct' (immutable) implementations of derived types.
Using reflection, the Kotlin class description (KClass) can be obtained using the ::class syntax on the instance you want to investigate (in your case, this::class in the initializer block of the abstract class). This gives you access to isData:
true if this class is a data class.
However, as Oliver points out, data classes can still contain var members, so you likely also want to check whether all member variables (and their member variables recursively) are defined as val to ensure immutability of all deriving classes.

Dart/Polymer binding object attributes of a list

I am trying to bind to the attributes of objects in a list. The list is made observable by using toObservable. Now I bind all the elements with polymer. Works fine. If a add an object to the list the update works also fine. But there is no update if the attributes of the objects in the list is changed. How can I achieve this?
I think you miss extends Object with Observable of the class declaration and/or the #observable annotation of the property declaration as shown below:
class YourListItem extends Object with Observable {
#observable aProperty;
}
If this doesn't help please provide the code of the objects you put into the list in your question.

How to make a class in Qt both scriptable and serializable?

I'm trying to write a class with two basic characteristics:
It needs to be scriptable - the class contains a number of properties and methods decorated with Q_INVOKABLE that are exposed to scripts.
It needs to be serializable so that it can be registered with qRegisterMetaTypeStreamOperators() for storing in QVariants.
As far as I can tell, I need to derive from QObject in order to make the class scriptable. However, in order to register the class with qRegisterMetaTypeStreamOperators(), it seems like the class needs to have a default copy constructor - something a QObject-derived class cannot have.
Is there any way to achieve both goals?
You can have scriptable objects not derived from QObject but it's more work. It's discussed here

Should I use a singleton class that inherits from an instantiable class or there's another better pattern?

I've got a class called ArtificialIntelligenceBase from which you can create your own artificial intelligence configuration sending some variables to the constructor or you can make a class that inherits from ArtificialIntelligenceBase and in the constructor of this new class just call the function super() with the parameters of the configurations.
I've also created some examples of artificial intelligences in classes, AIPassive, AIAgressive and AIDefensive. Obviously all of them inherits from ArtificialIntelligenceBase.
The point is that there're only few public functions in the base class. The variables in the base class are read only and the non public functions are protected in case you need to apply some modifications on them when created another pre-defined AI.
You can also create another AI just calling the base class sending some parameters in the constructor like this: new ArtificialIntelligenceBase(param1, param2, param3, param4);
I've tought about make the classes as a singleton because the classes can never change and once setted, their variables never change.
The question is: Is the singleton the best pattern to do this? Because I'm not sure.
PD: You don't need to explain any patter, just mention the name and I'll search for how it works
PPD: I'm developing in AS3. Just in case it helps
Thanks
In general, singletons are evil. I don't see any reason in your case to use a singleton, either. It sounds like you're using your own version of a factory method pattern (using a constructor somehow?) or maybe a prototype (I don't know AS3 one bit), but if you're looking for other patterns a couple of other ones are abstract factory and builder.
You don't need to use the singleton pattern to limit yourself to using only one instance per type of class, though. It doesn't help avoid redundancy.

Resources