Need help passing variables from one sub classes to another in scala - scala

So, I've created an abstract class and have multiple sub classes extending it. One of my sub classes holds all of my methods that I need as well as some global variables and from those variables one of them is an accumulator. Is it possible to carry over the information stored in my accumulator variable into a different sub class ? If so how would I go about doing so.

Related

Difference between objects, attributes, variables and class instance

I am having trouble understanding my professor's lecture notes because my brain seem to treat objects, attributes, variables and class instance as interchangeable. I really appreciate any help in distinguishing these 4 terms. Thank you!
this would be helpful for u Visit https://www.quora.com/What-is-the-difference-between-instance-variable-and-class-variable
Class variables are declared with keyword static and Instance variables are declared without static keyword.
Class variables are common to all instances of a class. These variables are shared between the objects of a class. Instance variables are not shared between the objects of a class. Each instance will have their own copy of instance variables.
As class variables are common to all objects of a class, changes made to these variables through one object will reflect in another. As each object will have its own copy of instance variables, changes made to these variables through one object will not reflect in another object.
Class variables can be accessed using either class name or object reference. Instance variables can be accessed only through object reference.
https://qph.fs.quoracdn.net/main-qimg-c4b92e80a8500c11fe705c1bafc3ed26
You don't mention the programming language at question.
Usually a class is a model or template that declares
how a certain category of objects look like.
You give a class a name and you mention if it inherits
members from another class or not.
You define also the class members.
These can be variables that hold data (object state)
and methods (class defined functions) that define
the object behaviour.
When you instantiate a class using the declared model
, you get an object, that is a concrete class instance.
This is a concrete entity, think of it as a new variable in memory,
whose data type is the class (instead of for example
integer or string data types), whose value is its state
in a defined moment in time (the state being the
combination of all of its data member variables values
at that moment). This object has to have an identity,
because it exists in memory and it is a different entity
from the other objects you can instantiate from this or
any other class. The data member variables hold specific
values for each instance. These are not shared between
instances.
Now the member methods can be shared between instances
because they have no state, so they are equal for every object.
They are called with some arguments
and they do some action that changes the object state, or
is at least tightly related with the concrete object.
But they are common to every object. The methods usually
know what concrete object they act upon by means of a special
name like 'this' or 'self', that references to 'itself'.
Objects are usually assigned to variables upon creation,
storing a reference to its identity that allows the
remaining code to manipulate them.
You use these variables to refer to the concrete object
outside the code of the classes, and use 'this' or 'self'
to refer to it from inside the classes.
Frequently you access object members qualifying with the
object name. Like in 'player.run()', or 'player.total_score'.
That is if player is a variable to which you assigned a
class Player instance. This can look like player = new Player
or player = Player().
Attributes is just another name given to data members.
Sometimes attributes and also methods can be public or private,
meaning code outside the class can use them, or only
the class code can have access.
Sometimes you see data members or attributes referred as
properties. When you access an attribute, you are accessing
a property. In some languages like Python, property can mean
something a little different but close related anyway...
Now also depending on the language things can be like described
(C++, Java) or you can have everything being treated as objects,
including the class definitions (Python).
You should also search the internet or SO about
inheritance, overriding, class diagrams, and other things class
related.
This is all no more than the ability of defining your own data types
beoynd the language builtin types.
You can think of variables as names for boxes (memory containers in a certain address) holding values. But sometimes you want to manipulate
not the values but the addresses themselves. This time you say you have
references (to addresses). Sometimes variables are just names for those
references. References are also known as pointers. But you could do math with pointers (increment, decrement, add a fixed value to...) that you usually don't do with references.

MATLAB - How to compare if two objects are the same or different

In C++ I would just compare the memory addresses of both objects. How would I do something similar in MATLAB?
Worst Case would be to have a static variable that iterates in each constructor and every object gets the current value as ID. But is there a better solution?
Thank you in advance.
#Edit:
I'd like to extend this question by assuming I have some given/not changeable classes inheriting handle and overloading eq. If I want to compare two objects of this class can I somehow cast both instances to handle and use the implementation of eq of the super class?
To test that two handle objects a and b refer to the same instance, you only need to use a == b. This is the same as eq(a, b). This is the defined behaviour of == for handle objects. I.e., for handle objects, == tests for equality of instances, not equality of the values within the instances. This is different from value objects.
For this to work you need to be using handle objects (classdef myObject < handle) because it doesn't make sense to test instances of value objects.
N.B. if you also need to get some kind of instance identifier for a handle object, then you need to do something like you describe using a persistent variable. Here's an example. In that case I would make that a base class for all your objects, so you wouldn't have to copy the same code into each class. But that's unnecessary if all you want to do is test two instances.

Static variable in ObjectiveC

Static variables are variables allocated statically at compile time. My doubt is for what purpose some variables are declared statically? I didn't have used any static variable in my code till now. From the apple code http://developer.apple.com/library/ios/#samplecode/CryptoExercise/Introduction/Intro.html
in securityWrapper.m, there is a line
static SecKeyWrapper * __sharedKeyWrapper = nil;
what is the use of statically allocating __sharedKeyWrapper here?
I have seen the use of static key word in so many codes. So kindly explain the use and benefits when static keyword.
Thanks in advance.
That file global is a singleton, used for sharing an instance of a class.
The reason it's static in Objective-C is internal linkage. In practice, this means the variable can not be seen outside the file it is declared in. If another file declares a variable with the same name, they're two different variables.
Keep in mind that the way Objective-C instances work, the instance won't actually be allocated automatically. Rather, you have a pointer to the instance. The code will still have to do something like:
if ( !_sharedKeyWrapper ) {
_sharedKeyWrapper = [[SecKeyWraper alloc] initBlahBlah];
}
See links for more.
When you define a new class of objects, you can decide what instance
variables they should have. Every instance of the class will have its
own copy of all the variables you declare; each object controls its own
data.
However, you can't prescribe variables for the class object; there are
no "class variable" counterparts to instance variables. Only internal
data structures, initialized from the class definition, are provided for
the class. The class object also has no access to the instance variables
of any instances; it can't initialize, read, or alter them.
Therefore, for all the instances of a class to share data, an external
variable of some sort is required. Some classes declare static variables
and provide class methods to manage them. (Declaring a variable static
in the same file as the class definition limits its scope to just the
class-and to just the part of the class that's implemented in the file.
Unlike instance variables, static variables can't be inherited by
subclasses.)
Static variables help give the class object more functionality than just
that of a "factory" producing instances; it can approach being a
complete and versatile object in its own right. A class object can be
used to coordinate the instances it creates, dispense instances from
lists of objects already created, or manage other processes essential to
the application. In the case when you need only one object of a
particular class, you can put all the object's state into static
variables and use only class methods. This saves the step of allocating
and initializing an instance.
Also static variables are initialized once. You can use static variables in recursive calls. Simple Example Factorial.

Limiting input to a vba class interface

I am trying to encapsulate the access of an XML configuration file within a (excel) vba custom class. A portion of the XML document is split into 4 or 5 repeated sections that are differentiated by an AreaID. MY question is: How can I limit the input of one of my class interfaces to the various AreaID's that may be read in from the XML document?
revision: what is the best way to control input into a class' interface from within the class (as it pertains to vba)? (i.e. public enum, secondary "helper" class, error handling from within the class, hard-coding...)
I wanted to follow up with #Tim's comment regarding enums and provide some further information as to why enums in the class code may be a good way to limit the input. According to Chip Pearson's site :
Enums cannot be declared within a procedure. They must be declared within the declarations part of a module, above and outside any procedure in the module. Class modules can declare Public Enum types but you cannot create a variable whose type is an Enum declared within a class module.
So as I understand this, if you declare the input on one of interfaces as a (public) enum that has been declared in the class itself, it will limit what values can go in based on that enum. Also the enum will still not be able to be used outside of the class structure.

When should I make a Moose class immutable when I'm actually using ->meta?

Normally I would finalize a class at compilation via __PACKAGE__->meta->make_immutable at the end of the class. However, when should I be making a class immutable that composes roles into itself at runtime? Should I even be doing so to get better performance or is this imcompatible with make_immutable? make_immutable seems to speed up instantiation of the object, but does it do anything once the object is instantiated?
For example, something along the lines of:
BUILD {
my $self = shift;
use Module::Load;
### Use the arguments passed in to determine roles applicable to
### this instance of the object. Load and apply roles.
for my $role ($self->_determine_roles()) {
load $role;
$role->meta->apply($self);
}
### $self is now a Class::MOP::Class::__ANON__... anonymous class
### Should I then be saying I'm done mutating it with something like this?
### can make_immutable even be run on an object instance and not a package?
$self->meta->make_immutable;
}
Even if the code above works for a single package, what happens when a object reblesses itself with the 'Foo' role, resulting in a anonymous class, then a second object blesses itself with a 'Foo' (becoming that same anon class) then 'Bar' role? Is it going to work correctly when the second object blesses itself into the immutable first anonymous class, then tries applying the role to the now-immutable anonymous class to create a new anonymous class?
From reading the docs on Moose::Meta::Class, it appears only a class can be immutable, not instances of objects. If so, should I just be ignoring make_immutable as I am mutating my classes?
You should be doing make_immutable as usual at the bottom of your class, and not worrying about it at all in your BUILD.
When you apply a role to an instance at runtime it doesn't modify the instance's class to apply the role (that would be messy and horrible and affect all other instances of that class); it creates a new anonymous class that inherits from your class and does the requested roles as well, and then reblesses the instance into that class. Since the original class isn't being modified there's no issue of whether it's open/mutable or not.
You actually can do $self->meta->make_immutable after the role application — it will immutabilize the newly-created anonymous class — and for completeness you probably should. But it will only give a small benefit, since most of what make_immutable does is to make the constructor faster, and the new class's constructor doesn't run anyway.
If you want to see the details of how role application to instances works, you should peek at the source of Moose::Meta::Role::Application::ToInstance.