Dart - How to access global scope? [duplicate] - flutter

This question already has answers here:
How can I call a function from a method of the same name?
(3 answers)
Closed 12 months ago.
The simple actual question is
How to access GLOBAL scope in Dart?
Below is just the answer why I'm looking for it.
I have a problem which requires me to learn about Dart's scope resolution from here but unfortunately I can't find the solution for my problem, not even from Google or here, which was surprising me to have to open this question.
Dart allows user to exclude the this. on member variable access within class's scope so if two potentially conflicted variables need to share the same name, they need to be differentiated with scope operator and reference.
I need to define a class with member variable print where there is also global function print being called somewhere within the class. Of course print() => print(...) will returns error so I'm expecting something as simple as print() => global.print(..) but neither that is working.
I know there's workaround to solve it but please I need a straightforward one if any. Thank you.

you could add this line at the top of your file:
import 'dart:core' as core;
Then you would be able to refer to print as core.print(...);
The problem is that now you would have to use core. for every primitives like int, example:
core.int variable = 1;
I think this is not worth it and it's better to use another name for your print method.

Related

What is the point of adding 'new' in front of a widget in Flutter? [duplicate]

This question already has answers here:
Do you need to use the "new" keyword in Dart?
(3 answers)
Closed 2 years ago.
For instance,
return new Container(child: new Text('Hello'),);
VS
return Container(child: Text('Hello'),);
I see people use both and was just wondering what the point is. Is it possible to use the same widget again somewhere or something?
the "new" keyword was once required in dart (the language used for flutter) for object instantiation, but this requirement was removed.
In order to prevent breaking old code, the keyword remains but is now optional. There is no difference between using it or not using it.
You may choose to use it if you want to make it clear that you are intending to do object instantiation and not call a function, as attempting to do new somefunction() //not a constructor will fail, though this is optional.

Understanding = operator in dart

What does = do here?
List<Segment> totalSegments = flight.departureFlight.segments;
Do both, totalSegments and flight.departureFlight.segments point to the same memory reference or totalSegments has the same data as flight.departureFlight.segments but points to a different memory location?
My understanding was that the latter should happen since dart is pass by value and not reference. However, a very annoying bug occurred when I added this line below that one:
totalSegments.addAll(flight.returnFlight.segments);
This above line actually modified the flight variable which in turn somehow modified the AsyncSnapshot from the StreamBuilder. Although, I wasn't using the variable anywhere else and not modifying other variables mentioned.
This all happened inside build function of a Stateless Widget. Maybe that has to do something with it.
I tried reading dart documentation for it, but either I couldn't find what I am looking for or the information is simply missing there. Read this too, but according to this, my use case shouldn't happen.
When it comes to objects as in your case, you are assigning a reference to an existing object (memory location) to a new variable. While acting upon that reference, you change the same object.
If this is not what you intend, check out answers related to (deep) copying of the objects
You were mistaken by the fact that Dart passes by value, and not by reference. Actually, it is exactly the opposite: (Almost) everything is always passed by reference (Which is usually a good thing!) Therefore, it is quite logical that because you edited totalSegments your departureflight.segments got edited too. It is a synonym. One of the ways to solve your problem would be:
List<Segment> totalSegments = List();
totalSegments.addAll(flight.departureFlight.segments.toList());
List<Segment> totalSegments = flight.departureFlight.segments;
This expression does the following.
Assigns the value of the expression flight.departureFlight.segments to variable totalSegments.
This and only this and nothing more.
There is no need to know what is really happening, because this is what happens.
P.S.
What value will be obtained as a result of executing the expression flight.departureFlight.segments is another question, because it depends on the implementation of the members of the operands of the expression flight.departureFlight.segments.

How to call a helper function from a library class [duplicate]

This question already has answers here:
How do you load a helper within a library in codeigniter?
(2 answers)
Closed 12 months ago.
I have a library class "Weekprint.php" (libraries/Weekprint.php) and in a helper file (calendar_helper.php) I have function like
print_calendar($start, $events, $cal_type).
I can use them separately,
I wonder if/how I can call print_calendar from Weekprint.
I use Codeigniter 3.1.5
You can call the helper from the library. The helper needs to be loaded before it can be used. Pick the logical spot in Weekprint.php to do that in the usual way.
$this->load->helper('calendar_helper');
After that, you can call it in the usual way when needed.
print_calendar($start, $events, $cal_type);
Obviously, the various arguments to the helper function need to be assigned values.
I could do it,
$CI = &get_instance();
$CI->load->helper('calendar_helper');
Inside the library Weekprint.php

How to access a base workspace variable from matlab app designer?

I have a variable ( cell array) created in base workspace in matlab..i am building an app in appdesigner to access the base workspace variable and find the transpose at push of a button.
when i tried to use the variable in callbackfunction i get the error :
undefined function or variable even though the variable defined in base workspace.
Kindly help me with a solution.
I literally just got Matlab 2016 yesterday, and I haven't tried what you're attempting, but I'm guessing it's similar to similar scenarios in functions. So I guess you can try this:
localVariableInCallback = evalin('base','nameOfVariableInWorkspace');
Where
localVariableInCallback is a local variable in the callback function
nameOfVariableInWorkspace is the variable in your base workspace
Reference: Matlab 'evalin' documentation: https://uk.mathworks.com/help/matlab/ref/evalin.html
I noticed you posted same question twice, and was downvoted. I strongly recommend that you have a look at what they suggested and provide a Minimal, Complete, and Verifiable example for your questions.

How to prefix/suffix identifiers within a macro? [duplicate]

This question already has answers here:
Is it possible to declare variables procedurally using Rust macros?
(4 answers)
Closed 5 years ago.
When using a macro that defines a function, is it possible to add a prefix to the function?
macro_rules! my_test {
($id:ident, $arg:expr) => {
#[test]
fn $id() {
my_test_impl(stringify!($id), $arg);
}
}
}
For example, fn my_test_$id() {
I'm defining tests using an identifier which may begin with numbers, and I would like to use a common prefix.
Currently this is not supported in stable.
However there is a feature in nightly called concat_idents:
concat_idents!(my_test_, $id)
See
Rust docs
Issue
Update: it seems there aren't near-term plans to add this into stable releases, see issue.
[...] is it possible to add a prefix to the function?
No. Really, really no. Super totally not at all even in the slightest.
I would like to have use a common prefix.
Put them all in a mod instead.
As mentioned, you should use submodules for this, but remember that macros can create submodules, submodules can be nested allowing their names to overlap, submodules can provide impls, and the tests submodule is not magic.
I once submitted a pull request that avoids numerous "boiler plate names" by refactoring the code using these tricks, although the #[no_mangle] exports make it harder.