Is it possible to edit the value of a public variable from another module? - swift

Can you edit the value of a public variable from another module or does that variable have to be open.
My understanding of public/open in swift 3 is:
Public classes can be made in another module but only open classes can be subclassed in another module.
Public functions can be called in another module but only open functions can be overwritten in another module.
But I am unsure about about variables.

You most definitely can! Here is a great way you can accomplish it:
In the module that contains the variable that you wish to manipulate:
// Must be declared globally
public var someValue = "hi"
In a different module that you wish to manipulate the variable from:
// Initialize the module that holds the variable you want to manipulate
var myModule = SomeModule()
// Manipulate the variable (someValue)
myModule.someValue = "bye"
The variable someValue will now have a value of "bye"

Related

octave class member variable change not working

I have set up a tiny test class with one member variable. I am trying to change this variable within a member function of the class.
I have named the class: "Test"
I've created a new folder: #Test where all methods are inside. The folder looks like this:
In Test.m (the constructor) there is the following code:
function obj = Test()
#member variable
mem.memory = [1,5,2,4,3];
obj = class (mem, "Test");
endfunction
Then I also have the "display" method:
function display(obj)
obj.memory
endfunction
And the method to change the member variable:
function change(obj)
obj.memory = [9,8,7,8,9];
endfunction
the last thing I have is the main script, here I'm creating a new object of the class Test, display it, change it, display it again. This file lies one folder above the #Test folder:
clc
clear all
c = Test();
display(c);
change(c);
display(c);
Here is the output of the program:
Like you can see, the values didn't change to 9 8 7 8 9. It feels like the variable is set to const, but also there comes no error, like: you can not change const variables...
I looked online for some examples, but only could find stange things...
It would be nice, if someone can link me a good tutorial about classes.
You need to change this to
function obj = change(obj)
obj.memory = [9,8,7,8,9];
and then call the method as
c = change(c);
In the MATLAB language, all function inputs are taken by value, not by reference (with the exception of handle classes). Thus, changing the copy of obj inside the function does not change the object in the caller workspace. The copy needs to be returned and assigned to the original variable.
MATLAB does optimize the syntax to not actually make any copies. I think Octave does the same, but am not sure.

Extending the prototype of a built-in class in Typescript 2.8+

This doesn't work
interface String {
contains(s:string):boolean;
}
String.prototype.contains=(s:string):boolean=>this.indexOf(s)!==-1;
because Property 'contains' does not exist on type 'String'
This is a bit of a surprise since adding it was the entire point of the interface declaration. http://www.typescriptlang.org/docs/handbook/declaration-merging.html suggests that the above code is legal. String is in the global namespace as far as I can tell by examining lib.es2015.wellknown.d.ts.
What's the right way to go about this? After reading Aluan Haddad's Extending third party module that is globally exposed I rewrote like this
declare global {
interface String {
contains(s: string): boolean;
}
}
String.prototype.contains=(s:string):boolean=>this.indexOf(s)!==-1;
and the interface change is now correct. But now 'this' implicitly has type 'any' because it does not have a type annotation.
Per further comments this can be explicitly typed using function syntax.
String.prototype.contains = function (this: string, s:string):boolean {
return this.indexOf(s)!==-1;
};
It should also be noted that in the course of this investigation I discovered that contains is implemented with the name includes and is declared in lib.es2015.core.d.ts
If you're defining the augmentation inside of a module, that is a file containing a top-level import or export then you need to use a declare global block in order to augment the global scope. Otherwise, the interface that you declare will not be merged into the global array interface because it's local to the module like any other declaration would be. The declare global syntax is specifically to cover this use case.
Furthermore, when you define the actual method you can't use an arrow function if the method itself is defined in terms of this because Arrow functions have a statically scope this while a dynamic this is needed for methods.
Putting it together
// this is a module
export {}
declare global {
interface String {
contains(other: string): boolean;
}
}
String.prototype.contains = function (other) {
return this.indexOf(other) and !== -1;
};
Note that whether the type being augmented is a class or an interface, the member needs to be declared in an interface as above because interfaces can merge with classes and interfaces can merge with interfaces but classes do not merge.

Stopping reference variables changing the value of the original variable

I am assigning the value of a custom class to another variable. Updating the value of the new variable is affecting the value of the original variable. However, I need to stop the reference variable from updating the original variable.
Here's a basic representation of what's happening:
var originalVariable = CustomClass()
originalVariable.myProperty = originalValue
var referenceVariable = originalVariable
referenceVariable.myProperty = updatedValue
print("\(originalVariable.myProperty)") //this prints the ->updatedValue<- and not the ->originalValue<-
I've tried wrapping the referenceVariable in a struct to make it a value type but it hasn't solved the problem.
I've found information regarding value and reference types but I haven't been able to find a solution.
My question in a nutshell: How do I stop an update to a reference variable from updating the original variable that it got its value assigned from?
Thanks in advance.
The whole point of reference semantics (as used by classes) is that all variables point to the same (i.e., they reference the same) object in memory. If you don't want that behaviour, you should use value types (Struct, Enum, Array...) or create copies of your object.
If CustomClass implements the NSCopying protocol you can do:
var referenceVariable = originalVariable.copy()
If it doesn't, you'll have to find some other way to copy it or implement the protocol yourself.
Wrapping the class in a struct will just make two different structs each containing a different reference to the same object.

Accessing properties within powershell class

I have a need to access common properties within my class functions and, unfortunately, outside my class in other functions.
For example
Class Myclass {
[Void]my_function(){
$file_location = "$myvar"
}
}
properties { # some props
$myvar = "somefile.txt"
}
function do_things{
echo $myvar
}
When I attempt to access $myvar within my_function as part of the class, I get a parsing error "variable is not defined in the method" which makes sense as the variable isn't declared in the class, but I would imagine things in a properties block would be usable.
I'm hoping there is something I"m not aware of and haven't been able to find in the documentation that allows me to do this. It seems rather silly that I can't use properties within my class functions.
My current (unfavorable) solution is to have my class inherit from another class that just has a bunch of static variables defined in it since you can access those anywhere else in the script by doing [MyOtherClass]::static_variable_name. This just isn't how I'd prefer to do it.
Please advise
I can not provide reference right now, but I remember reading in some PowerShell blog, that purpose of this feature is to make PowerShell classes to be more self containing and to catch error early, for example, if you misspell variable name.
Parser simply disallow you to reference random variables in class method body directly, unless you prefix it with global or script scope. But, apparently, no technical restriction was made to prevent you from referencing variables in any other way.
So that, you can ask Get-Variable to read variable value for you:
Get-Variable myvar -ValueOnly
And you even can make this, because nested script blocks are not analyzed:
&{$myvar}

Define a typealias for the whole project

In which file can I define a typealias that works in the whole project, etc.
typealias S = String
the typealias works the same way as variables. If you want to be global put it outside the class scope at any file.
typealias is use to aliasing the existing Data Types. Its access scope is same as a variable, if its declared in side the block like class, struct, protocol etc then its visibility also remains inside that block only. it can not be used outside to declare a new variables of that type.