Swap an AMD Module Dependency in Dojo - dojo

Here is a widget class declared in a MyWidget.js module.
define(["dojo/Foo","myapp/Bar"], function(Foo, Bar) { return declare('MyWidget', [], {
postCreate:function() {
var bar = new Bar();
bar.sayHello();
}
})});
In this theoretical example, "myapp/Bar" is a class defined similarly by returning a declare call. Now let's assume that I have created "myapp/SpecialBar" by extending "myapp/Bar".
In another widget, I want to tell MyWidget to use "myapp/SpecialBar" instead of "myapp/Bar" like so:
require(["myapp/MyWidget","myapp/SpecialBar"], function(Foo, SpecialBar) {
//Now swap "myapp/Bar" module dependency of "myapp/MyWidget" to "myapp/SpecialBar"
var myWidget = new MyWidget();
});
I know ways to do this. For example, I could add a Bar attribute to "myapp/MyWidget" and assign the value of the Bar module. This would allow me to instantiate like this: new MyWidget({ Bar:SpecialBar }). However, this seems like too much ceremony. Is there a clean way to just swap an AMD dependency without any special treatment to the module definition?

That is the clean way. You cannot change the modules that a widget/module depends on, well, you could map them, but that's done globally, so then it always maps to a specific module.
If you could do that, you could break a lot of stuff as well, besides, such a feature does not exist in any language. Comparing require() with imports in Java and .NET, you will see a similar trend.
The only way to change the module, is by changing the behavior/state of the module, which means by overriding properties or functions. When a module is "swappable" it's often used as a property, examples where this occur:
The dojo/dnd/Moveable class allows you to set a custom dojo/dnd/Mover through the mover property. In this case the constructor is added as a property to the Moveable (reference guide)
All widgets with a dropdown inherit from dijit/_HasDropDown, which adds the dropdown widget itself as a property to the parent widget

Related

How can I count the widgets stored in a Gtk::Grid?

I am looking for several gtkmm methods/data types that are equivalent to some QT expressions:
given a widget container in QT (e.g. QBoxLayout), a simple method called count() returns the amout of widgets stored in the given container. The best way to exchange QBoxLayout or any QT widget container is Gtk::Grid. But there is no simple way to get the amount of widgets inside of it. Same with Gtk::Box.
I need that method to iterate the childs stored in the grid:
for(auto& it : layouts) { //layouts is a vector of Gtk::Grids
for(int i = 0; i < it->size(); ++i) {
if(it->get_child_at(0, i)) {
it->get_child_at(0, i)->set_visible((std::string(it->get_name())== StringID));
}
QT uses QWidget as an object unbounded to any widget type (such as a button or a checkbox..). That is not possible in gtkmm, because Widgets can only be initinalized with a reference to a widget type. Now I'm looking to replace a QT code with gtkmm, that has a vector of widgets. I used Gtk::Box to replace the QWidgets. Is that a reasonable replacement? I'm getting trouble replacing their scale, which was originally dealt with using QSize, expecting two numbers "hight" and "lenght". Now there is a Gtk::Scale class, but it works in a different way..
All Gtkmm containers (like Gtk::Grid) inherit from the Gtk::Container, which makes the following methods available:
std::vector<Widget*> get_children() // Non const: to modify the widgets
std::vector<const Widget*> get_children () const // const: for read operations
These methods return exactly what you want: a vector of widgets. You can use size() on the returned std::vector to count the number of wigets in the container.
Also, in my opinion, Gtk::Box is very rarely useful since it is very limited. Gtk::Grid is almost always a better choice.
Finally, in Qt, all graphical elements inherit from QWidget. In Gtkmm, all widgets inherit from Gtk::Widget, meaning that you can write something like:
Gtk::Widget* pButton = new Gtk::Button("My button");
and then use pButton, which is "unbounded to any widget type". See the reference for more information on Gtk::Widget.
Update for Gtkmm4
It seems Gtk::Container was removed in Gtkmm4 as documented here. There seems to be nothing to replace it. Unfortunately, it looks like in this version, child widgets tracking will have to be done manually.

Optaplanner: prevent custom List from beeing cloned by FieldAccessingSolutionCloner

I have a #PlanningSolution class, that has one field with a custom List implementation as type.
When solving I run into the following issue (as described in the optaplanner documentation):
java.lang.IllegalStateException: The cloneCollectionClass (class java.util.ArrayList) created for originalCollectionClass (class Solution$1) is not assignable to the field's type (class CustomListImpl).
Maybe consider replacing the default SolutionCloner.
As this field has no impact on planning, can I prevent FieldAccessingSolutionCloner from trying to clone that particular field e.g. by adding some annotation? I dont want to provide a complete custom SolutionCloner.
When inspecting the sources of FieldAccessingSolutionCloner I found out that I only needed to override the method retrieveCachedFields(...) or constructCloneCollection(...) so I tried to extend FieldAccessingSolutionCloner but then I need a public no-args-constructor. There I dont know how to initialise the field solutionDescriptor in the no-args-constructor to use my ExtendedFieldAccessingSolutionCloner as solution cloner.
If the generic solution cloner decided to clone that List, there is probably a good reason for it do so: one of the the elements in that list probably has a reference to a planning entity or the planning solution - and therefore the entire list needs to be planning cloned.
If that's not the case, this is a bug in OptaPlanner. Please provide the classes source code of the class with that field and the CustomListImpl class too, so we can reproduce and fix it.
To supply a custom SolutionCloner, follow the docs which will show something like this (but this is a simple case without chained variables, so it's easy to get right, but solution cloning is notoriously difficult!).
#PlanningSolution(solutionCloner = VaccinationSolutionCloner.class)
public class VaccinationSolution {...}
public class VaccinationSolutionCloner implements SolutionCloner<VaccinationSolution> {
#Override
public VaccinationSolution cloneSolution(VaccinationSolution solution) {
List<PersonAssignment> personAssignmentList = solution.getPersonAssignmentList();
List<PersonAssignment> clonedPersonAssignmentList = new ArrayList<>(personAssignmentList.size());
for (PersonAssignment personAssignment : personAssignmentList) {
PersonAssignment clonedPersonAssignment = new PersonAssignment(personAssignment);
clonedPersonAssignmentList.add(clonedPersonAssignment);
}
return new VaccinationSolution(solution.getVaccineTypeList(), solution.getVaccinationCenterList(), solution.getAppointmentList(),
solution.getVaccinationSlotList(), clonedPersonAssignmentList, solution.getScore());
}
}

How to add an outside callable member functions to an anonymous object?

This is the first time I ask a question, please excuse my mistake on asking a question, and tell me how to ask a better question if that happens.
What I am asking:
How can I add a member function, into an anonymous object, which is callable outside this object? Or this requirement is too strange that shouldn't be implemented?
Consider this object expression below:
variable = object : AClass()
{
// inside the declaration of anonymous object
override fun aFunction(i: Int)
{
// do something inside aFunction
}
}.apply {
// do something inside apply
}
How can I add a function fun bFunction(i: Int), that just belongs to variable, not belongs to AClass, that can be callable by directly using
variable.bFunction(1)
? Or, it is impossible to do so?
What I have tried (and, of course, not satisfy what I want):
I can create an extension function fun AClass.bFunction(i: Int) to solve it, however this maybe "unfair" to other instance of AClass, since they actually don't need that.
I can create a class instead of using an anonymous object locally, however it seems too heavy to create a class for ONE variable.
I have tried adding bFunction inside the declaration of anonymous object, where the position is mentioned above. However I can just only access this function inside .apply {}, not outside this assignment.
I have tried adding bFunction inside .apply {}, however it also make me just access it inside .apply {}, not outside this assignment.
To be more specific of the original question (which is solved by myself, while typing this question) to avoid an X - Y question:
I am struggling on painting on Java GUI components. What I want to achieve is like a "canvas" or a "paper" inside a JFrame, where I can paint anything I like on it, by pixel control, totally using Kotlin.
After some searching, I found that the core solution to the question of, "if I draw something by using contentPane.graphics.drawXXX series function, this draw will be disappear when (I resize or minimize this JFrame, or just called this draw function too early, when this JFrame is showing (note: we can use Thread.sleep to delay too-early call))", is by overriding paint function. Then, in order to override it, I use var image = BufferedImage(...) to store what I have painted, and use an anonymous object
painting = object : JFrame()
{
override fun paint(g: Graphics?)
{
contentPane.graphics.drawImage(image, 0, 0, this)
}
}.apply { /* do some initialization */ }
to override paint function, and draw this buffered image into contentPane, which seems achieved what I want, and just a "static" painting.
I was wondering how can I add a updatePainting() function to this painting, which make me can just modify this buffered image, then call this function to update the painting, as a "dynamic" painting. However, during the time I am typing this question, I accidentally find that, just modify this image, will automatically update this painting, no need to manually update this painting. So this original question actually solved: this bFunction is not necessary in my current situation. But, I just wondering, are there any way to implement this things that can fit this (may be strange) issue?
Actually, this code (with bFunction inside the declaration of anonymous object) will work perfectly well and will make bFunction accessible outside... but only if variable is local or private. In both cases it has to be initialized like this together with declaration, i.e. val variable = object : AClass() ....
Public/protected properties need a type which can be used outside the class which declares them, unsurprisingly. In that case you'll have to name the subclass.
The page you link covers this
Note that anonymous objects can be used as types only in local and private declarations. If you use an anonymous object as a return type of a public function or the type of a public property, the actual type of that function or property will be the declared supertype of the anonymous object, or Any if you didn't declare any supertype. Members added in the anonymous object will not be accessible.

Why can't I extend the static Math class?

I'm working on an ActionScript 2 project that relied on some extensions to the Math class, usually done as such:
Math.sinD = function(angle) {
return Math.sin(angle*(Math.PI/180));
};
Allowing a caller to just write (for example)
Math.sinD(60);
However, when I try to compile with just the original line, I get the following error:
There is no property with the name "sinD".
Why isn't this working, and more importantly, how can I make it work again?
To my mind looks impossible.
Create static sinD method in separate class.
The 'problem' lies with ActionScript 2's static type checking. You can't directly modify a class because it checks that the property you're trying to access exists. However, you can do it if you bypass the type checking:
var Math2 = Math; // thing now holds a reference to the same Math class, but with no type set
// Add a new method
Math2.sinD = function(angle) {
return Math.sin(angle*(Math.PI/180));
};
// Now the Math has 'sinD' as a method
Or alternatively... (these both do the same thing)
Math['sinD'] = function(angle) {
return Math.sin(angle*(Math.PI/180));
};
However, this doesn't really help, because the static type checking is enforced at the calling places as well, so you can access any dynamically added methods.
Math.sinD(30) // This will not compile
Instead, you need to do:
var Math2 = Math;
Math2.sinD(30) // returns 0.5
Math['sinD'](30); // also returns 0.5
At which point, you may as well just create your own class rather than modifying the existing class.
Fortunately, for my case this means that there aren't any usages of these extensions (at least not statically typed usages), so I should be able to safely delete the extensions and not worry about it :)

How to get MATLAB to recognise newly added static methods?

I am using classes and static methods to 'scope' functions in a namespace, similar to C#. However, every time I add a new method to a class, at first it is not found. I have to restart the MATLAB environment (2007a) for the new methods to be recognised.
Surely there is an 'update' or 'refresh' type command that I can use so that I do not have to restart the MATLAB environment each time I add a function?
Issuing this call to CLEAR should do it:
clear classes
One unfortunate side effect of this is that it also effectively issues a clear all, which clears all of the variables in the workspace as well (however, this would happen anyway when you close and restart MATLAB). This clearing of the workspace actually serves a purpose, since it will remove any variables of the same type as the old version of your class, which potentially wouldn't work correctly with the new version of your class.
The function REHASH may work, but I doubt it (I think it deals more with file paths than class definitions).
Clearing instances of your class should work.
Suppose that you have an instance of "MyClass" in your base workspace:
foo = MyClass;
Now, suppose you edit MyClass and add new static method "bar":
foo.bar(); % Will cause error, as foo is instance of previous "MyClass"
However, "clear"-ing foo will remove the reference to the previous class:
clear('foo');
foo = MyClass;
foo.bar(); % this should now work.
This should be fine if you only have one or two instances of the class in your base workspace. If you have many instances of the class in your base workspace, then you may want to write a script to clear them:
varList = whos;
for iVar = 1:numel(varList)
if isequal( 'MyClass', varList(iVar).class )
clear( varlist(iVar).name );
end
end
clear('varList');
clear('MyClass');
If you have instances of the class in more locations, you may wish to extend the script as appropriate.
The last call to clear the class name might only be necessary if you are making modifications to classes in an inheritance hierarchy.
try "clear classname"