Finally, when I managed to understand how to fix this, that is, how to change the value of an internal dynamic variable, the code has moved on and now it is declared in this way:
my int $is-win = Rakudo::Internals.IS-WIN;
This is a class variable declared inside class Encoding::Builtin. Makes all the sense in the world, since an OS is not something that changes during the lifetime of a variable. However, I need to test this code from other OS, so I would need to access that class variable and assign it a True value. Can I do that using the meta object protocol?
The concept of "class variable" doesn't exist in Perl 6.
The declaration being considered is of a lexical variable, and its lifetime is bound to the scope (bounded by curly braces) that it is declared within. It doesn't have any relationship with the class that's being declared, so there's no way to reach it through the MOP. (That the block in this question happens to be attached to a class declaration is incidental so far as lexical variables go.) Nor is it declared our, so it's not stored in the package either.
The only way a lexical can be accessed - aside from under a debugger - is if something inside of that lexical scope explicitly made it possible (for example, by acquiring a pseudo-package and storing it somewhere more widely visible, or by allowing EVAL of provided code). Neither is happening in this case, so the variable not possible to access.
Perl 6 is very strict about lexical scoping, and that's a very intentional part of the language design. It supports the user in understanding and refactoring the program, and the compiler in program analysis and optimization. Put another way, Perl 6 is a fairly static language when it comes to lexical things (and will likely come to do much more static analysis in future language versions), and a dynamic language when it comes to object things.
Related
After using Java for a long time I almost forgot how it is to be allowed to define global variables. I read that it is recommended to use top-level functions in Kotlin instead of wrapping them inside objects like in Scala. How about top-level variables (which I guess we call global variables)? What is the recommendation about using them? What are the pitfalls? How are they deallocated?
Global variables are supposed to be accessible from anywhere at any point in your program, so it would most likely be a bad idea to deallocate global variables, even if you could because it could lead to instability or crashes in your application.
Now Kotlin is a JVM language, it compiles to Java code and therefore is garbage-collected. As for how the garbage collector works, when a variable is no longer referenced by the program, it gets flagged for deletion and will be removed by the garbage collector later (not necessarily instantly).
Global variable's scope is the entire program so they are never garbage-collected. What could you do tho is to set its reference to null (if it is declared nullable) to allow at least the referenced memory to be collected.
The top-level variables that you are referring about aren't exactly free since Kotlin will compile them into a java class named with the file name anyway(that's how you can access kotlin file-specific functions inside java code), so they are just like static fields in Java. For a better explanation you could check this thread: Kotlin: Difference between constant in companion object and top level.
Now we know that top-level variables are compiled into static variables inside a Java class. Those don't get flagged for collection unless the program somehow drops the class loader associated to that class itself but that's something I can't speak of more since I never used that.
Now in the end, should you use global variables? They are usually considered a bad practice and this is in big part due to the fact that they can be referenced by any part of the program and it can be hard to remember when and why did they change, especially in large applications. If you keep track of their usages and use them in moderation then they are alright. Personally, if I have to use them, I define them inside a class as a companion object the same way you would define static variables in java, like this:
class GlobalData {
companion object {
var varEx= 0
const val constString = "CONSTANT GLOBAL"
}
}
I've read the spec but I'm still confused how my class differs from [our] class. What are differences and when to use which?
The my scope declarator implies lexical scoping: following its declaration, the symbol is visible to the code within the current set of curly braces. We thus tend to call the region within a pair of curly braces a "lexical scope". For example:
sub foo($p) {
# say $var; # Would be a compile time error, it's not declared yet
my $var = 1;
if $p {
$var += 41; # Inner scope, $var is visible
}
return $var; # Same scope that it was declared in, $var is visible
}
# say $var; # $var is no longer available, the scope ended
Since the variable's visibility is directly associated with its location in the code, lexical scope is really helpful in being able to reason about programs. This is true for:
The programmer (both for their own reasoning about the program, but also because more errors can be detected and reported when things have lexical scope)
The compiler (lexical scoping permits easier and better optimization)
Tools such as IDEs (analyzing and reasoning about things with lexical scope is vastly more tractable)
Early on in the design process of the language that would become Raku, subroutines did not default to having lexical scope (and had our scope like in Perl), however it was realized that lexical scope is a better default. Making subroutine calls always try to resolve a symbol with lexical scope meant it was possible to report undeclared subroutines at compile time. Furthermore, the set of symbols in lexical scope is fixed at compile time, and in the case of declarative constructs like subroutines, the routine is bound to that symbol in a readonly manner. This also allows things like compile-time resolution of multiple dispatch, compile-time argument checking, and so forth. It is likely that future versions of the Raku language will specify an increasing number of compile-time checks on lexically scoped program elements.
So if lexical scoping is so good, why does our (also known as package) scope exist? In short, because:
Sometimes we want to share things more widely than within a given lexical scope. We could just declare everything lexical and then mark things we want to share with is export, but..
Once we get to the point of using a lot of different libraries, having everything try to export things into the single lexical scope of the consumer would likely lead to a lot of conflicts
Packages allow namespacing of symbols. For example, if I want to use the Cro clients for both HTTP and WebSockets in the same code, I can happily use both, and refer to them as Cro::HTTP::Client and Cro::WebSocket::Client respectively.
Packages are introduced by package declarators, such as class, module, grammar, and (with caveats) role. An our declaration will make an installation in the enclosing package construct.
These packages ultimately exist within a top-level package named GLOBAL - which is fitting, since they are effectively globally visible. If we declare an our-scoped variable, it is thus a global variable (albeit hopefully a namespaced one), about which enough has been written that we know we should pause for thought and wonder if a global variable is the best API decision (because, ultimately, everything that ends up visible via GLOBAL is an API decision).
Where things do get a bit blurry, however, is that we can have lexical packages. These are packages that do not get installed in GLOBAL. I find these extremely useful when doing OO programming. For example, I might have:
# This class that ends up in GLOBAL...
class Cro::HTTP::Client {
# Lexically scoped classes, which are marked `my` and thus hidden
# implementation details. This means I can refactor them however I
# want, and never have to worry about downstream fallout!
my class HTTP1Pipeline {
# Implementation...
}
my class HTTP2Pipeline {
# Implementation...
}
# Implementation...
}
Lexical packages can also be nested and contain our-scoped variables, however don't end up being globally visible (unless we somehow choose to leak them out).
Different Raku program elements have been ascribed a default scope:
Subroutines default to lexical (my) scope
Methods default to has scope (only visible through a method dispatch)
Type (class, role, grammar, subset) and module declarations default to package (our) scope
Constants and enumerations default to package (our) scope
Effectively, things that are most often there to be shared default to package scope, and the rest do not. (Variables do force us to pick a scope explicitly, however the most common choice is also the shortest one to type.)
Personally, I'm hesitant to make a thing more visible than the language defaults, however I'll often make them less visible (for example, my on constants that are for internal use, and on classes that I'm using to structure implementation details). When I could do something by exposing an our-scoped variable in a globally visible package, I'll still often prefer to make it my-scoped and provide a sub (exported) or method (visible by virtue of being on a package-scoped class) to control access to it, to buy myself some flexibility in the future. I figure it's OK to make wrong choices now if I've given myself space to make them righter in the future without inconveniencing anyone. :-)
In summary:
Use my scope for everything that's an implementation detail
Also use my scope for things that you plan to export, but remember exporting puts symbols into the single lexical scope of the consumer and risks name clashes, so be thoughtful about exporting particularly generic names
Use our for things that are there to be shared, and when its desired to use namespacing to avoid clashes
The elements we'd most want to share default to our scope anyway, so explicitly writing our should give pause for thought
As with variables, my binds a name lexically, whereas our additionally creates an entry in the surrounding package.
module M {
our class Foo {}
class Bar {} # same as above, really
my class Baz {}
}
say M::Foo; # ok
say M::Bar; # still ok
say M::Baz; # BOOM!
Use my for classes internal to your module. You can of course still make such local symbols available to importing code by marking them is export.
The my vs our distinction is mainly relevant when generating the symbol table. For example:
my $a; # Create symbol <$a> at top level
package Foo { # Create symbol <Foo> at top level
my $b; # Create symbol <$b> in Foo scope
our $c; # Create symbol <$c> in Foo scope
} # and <Foo::<$c>> at top level
In practice this means that anything that is our scoped is readily shared to the outside world by prefixing the package identifier ($Foo::c or Foo::<$c> are synonymous), and anything that is my scoped is not readily available — although you can certainly provide access to it via, e.g., getter subs.
Most of the time you'll want to use my. Most variables just belong to their current scope, and no one has any business peaking in. But our can be useful in some cases:
constants that don't poison the symbol table (this is why, actually, using constant implies an our scope). So you can make a more C-style enum/constants by using package Colors { constant red = 1; constant blue = 2; } and then referencing them as Colors::red
classes or subs that should be accessible but needn't be exported (or shouldn't be because overlapping symbols with builtins or other modules). Exporting symbols can be great, but sometimes it's also nice to have the package/module namespace to remind you what stuff goes with. As such, it's also a nice way to manage options at runtime via subs: CoolModule::set-preferences( ... ). (although dynamic variables can be used to nice effect here as well).
I'm sure others will comment with other times the our scope is useful, but these are the ones from my own experience.
I have a global variable X in an winform application.
The variable X is used in different forms inside the application and I don't want it to be modified. It's not used as a parameter in the functions... so ByRef, or ByVal are not applicable.
It's used like that:
Declaration
dim X as whatever;
dim Y as whatever;
private sub SubExample(A as object)
'Do some staff
'Locally modifiy X
X = something else;
end sub
Main program
call SubExample(Y);
'After this, X should still have its original value
Any idea please ?
You can't protect a global variable (unless it has to be assigned only once, in that case it can be Const). By definition it's global so it's visibile by all classes.
I would avoid them every time it's possible because of that: you can't restrict their access to who really has to use it (as you found by yourself) and they couple all classes use them. Main problems I see with them are:
Testing: because they couple many (all?) classes they make code testing pretty hard. You can't really isolate a class or sub-system for testing.
Concurrency: they're free accessed by everything in any thread then you'll have concurrency issues and you'll need to make them thread-safe. A variable in VB.NET can be thread-safe (at least atomic read/write) only for primitive types.
Access: as you saw you can't restrict access to them. Even if you make them global properties you can just make them read-only but somewhere a write function/setter must exist (unless you're using them for singleton pattern or for other - few - corner cases).
Maintenability: your code will be harder to understand because implications won't be obvious and local.
What you can do to replace them with something more "safe"?
If you put them in a global class with Shared members just remove Shared and make them instance members. If they're in a Module just move them to a Class.
Make your class singleton (I would use a method instead of simple property to make this more obvious). This may or not be your case, you may simply create your object in your startup method.
Add a public property in each form will need them and when you create your form just set this property to class you previously created. According to effective implementation this may be or not a Context Object pattern.
If you have multiple sets of global variables (and each set has different users) you may need to create multiple classes (one class for each set of variables).
This is a pretty general method to quickly replace global variables, better way implies some deeper refactoring to make your code more OOP-ish but I can't say without a more complete view of your code.
As a low-tech solution, I would recommend using an unambiguous name like
Dim READONLY_X
as the name of your global variable. Then you are less likely to forget that you should not be writing a new value to it. When you feel the temptation to write the line:
READONLY_X = 2
it should ring an alarm bell. Wrapping inside getter functions etc (without the formalism of a class) seems like a kluge. But that's just an opinion.
As was said before, global variables are a pain; think carefully about the scope you want them to have, and whether there isn't another solution...
This is more of varification, but i want to be sure before i start altering some old code to clean it up.
If you have private varibales declared inside a module but outside a subroutine, when are these actually created. For example, this is how a module is set up:
'Local objects.....
'Function Main.....
'Subroutines.......
Private Constants..
Private variables..
More Subroutines...
If those variables are only used in one subroutine, should they be declared inside that subroutine or in the local objects or right outside the subroutine as they are now?
Thanks!
The CLR has no support for modules or module variables so modules become static classes and module variables become static fields.
As a rule, variables should be declared as close to the point they are used as possible. Their scope should also be as constrained as possible.
Turning a variable into a field is a pretty bad coding practice for several reasons:
It is extremely easy to make a mistake and reuse the same field in another part of your module, creating unexpected conditions.
You increase the lifetime of the objects in the variable significantly. Typically, once you exit the method, the variable is available for garbage collection. By turning it into a static field, the object will stay alive until it's replaced or the application terminates
Multiple threads will be able to see and access the same static field, potentially creating race conditions. Given how many things work asynchrously nowadays, this can be a significant problem
I do not have a background on C/C++. I just started learning Objective-C after a past using other languages.
In which situations should I use static declaration of a variable over regular ivars or properties? What do I gain doing this?
thanks
Global variables and functions
By default, all symbols (global variables and functions) are exported (made visible to code in other source files). If a global variable is declared static, it is not exported. That means it is only accessible to code in the current source file.
This is useful when you have a global variable that you want to restrict access to, and don't want to worry about a name collision. For example, if you wanted to maintain a counter to track how many instances of a class have been created, you could create a static int gInstanceCount. Since it is static, you would know that (1) no other code can modify the variable and (2) if some other file uses a global with the same name, there won't be any collisions.
Static declarations in header files
Note that when you put something in a header file, it is as if you copied-and-pasted that code into every other file that includes it. That means if you declare something as static in a header file, every file that includes it gets its own copy of that thing.
That means if you declare static int foo in Foo.h and then write execute foo = 4 in Bar.m, when you try to access that value in Other.m you won't necessarily get 4 back.
Local static variables
You can also define a local variable (inside a function or method body) as static. Normally, local variables are allocated on the "stack" which means they are created when your function is executed and deallocated when the function exits. If two threads enter the same function at the same time (or one thread makes a recursive call back into a function) each thread gets a fresh chunk of memory to work with, and anything it does won't affect any other thread.
However, a local static variable is stored on the "heap" instead. All executions of the function share that same memory location. Also, when the function ends, the value stays where it is. That's why a local static variable is often used in sharedInstance methods on Objective-C singleton objects.
In most ways, a local static variable basically acts like a global variable that can only be seen inside the function which declared it.
the static keyword is used to provide scope to global variables. Normally, global variables defined outside of a function have a public scope, and are visible to all .m or .c files in a project. making a variable static allows you to have a "global" variable which is scoped (visible) to only the .m or .c file containing the variable definition. This allows you to have a variable which can be shared by multiple functions in a single source file, while avoiding potential naming conflicts. Also of note, the extern keyword, which allows you to indicate that a specific global variable is initialized in a different source file but used in this source file.
The keyword static is overused in C. It means several different things. In some contexts, it just means that field is only understood in the remainder of that file (compilation unit). In other words, it can't be linked to from some other file.
In Java and C++, static class members are defined at the class level, not the individual object level, so the one value is shared by all objects of that class (or one of its subclasses). Unfortunately, IMHO, Objective C does not support this. Instead you use statics at the file level.
If you don't know if you need a static variable, you probably don't need one.
One reason you might use a static variable is to provide a class variable (that is, one variable for an entire class, rather than an instance variable, which each instance of a class would have it's own copy of).
This question (and its accepted answer) explain how one would use static variables to simulate a class variable in Objective-C.
First: "object" in this context refer to "c objects", in very easy words, something, that lives at runtime. It has nothing to do with objects in the sense of OOP or Objective-C. You can think of an object as a var.
static has nothing to do with scope. static on an extern identifier (= declared outside a block) has internal linkage. The results of linkage are defined in ISO/IEC 9899:TC3, section 6.2.2. Scope is defined in section 6.2.1.
Internal linkage means, in short words, that two identifier in different translation units (".m files") do not denote the same object, but different objects. Every translation unit has its "own" object.
If an identifier is not extern (= inside a block) declared with static storage class it has a lifetime from begin of the program execution until the program stops running.