TS variables type-block - variables

when I RUN
const name: string = "Eric";
console.log(Hello ${name}, would you like to learn some TypeScript today?);
following error appears.
prog.ts(6,7): error TS2451: Cannot redeclare block-scoped variable 'name'.
../../opt/node/lib/node_modules/typescript/lib/lib.dom.d.ts(17178,15): error TS2451: Cannot redeclare block-scoped variable 'name'.
I never used this lib.dom.d.ts the error is talking about..
SOS please ..
naming a variable in a new project, I expect variable "name" a valid one but error

Related

I am starting to code but I don't how why my variables are never defined despite I do it

For example, I define a variable with an integer and if I try to check the variable in the console it says "undefined".
Assuming you are talking about JavaScript. The reason you see it's undefined because: "This usually occurs when a variable is declared. Here the variable is assigned a memory or space by the JavaScript engine. Because of this, once a variable is declared, it takes a value of undefined even before assignment."
When you assign a value (even null) to the created variable, it will no longer be undefined anymore
https://scotch.io/courses/10-need-to-know-javascript-concepts/declaring-javascript-variables-var-let-and-const
Try assigning the value to variable when you define it.
Eg: int a=1;
in your console type let a = 5;
then type a.
your output will be 5. Why? because you instantiated the variable but never call it afterwards.

Flutter Fails on Run (or upgrade, or anything else).

I have been getting a massive error recently, upon Running any of my flutter applications in Intellij, Visual Studio, etc. I cannot perform git functions because of it either. Any help would be greatly appreciated.
(Some parts omitted - this snippet represents the 40k line code pattern)
Launching lib/main.dart on iPhone X in debug mode...
compiler message:
file:///Users/JohnnySmithh/Documents/flutter/packages/flutter/lib/src/widgets/editable_text.dart:439:9: Error: Type 'LayerLink' not found.
compiler message: final LayerLink _layerLink = new LayerLink();
compiler message: ^
compiler message: file:///Users/JohnnySmithh/Documents/flutter/packages/flutter/lib/src/rendering/proxy_box.dart:4474:20: Error: The method 'getLastTransform' isn't defined for the class 'invalid-type'.
compiler message: Try correcting the name to the name of an existing method, or defining a method named 'getLastTransform'.
compiler message: return _layer?.getLastTransform() ?? new Matrix4.identity();
compiler message: ^
compiler message: file:///Users/JohnnySmithh/Documents/flutter/packages/flutter/lib/src/rendering/proxy_box.dart:4501:18: Error: Method not found: 'FollowerLayer'.
compiler message: _layer = new FollowerLayer(
compiler message: ^^^^^^^^^^^^^
compiler message: file:///Users/JohnnySmithh/Documents/flutter/packages/flutter/lib/src/rendering/texture.dart:76:7: Error: No named parameter with the name 'rect'.
compiler message: rect: new Rect.fromLTWH(offset.dx, offset.dy, size.width, size.height),
compiler message: ^^^^
compiler message: file:///Users/JohnnySmithh/Documents/flutter/packages/flutter/lib/src/rendering/view.dart:124:11: Error: 'ContainerLayer' isn't a type.
compiler message: final ContainerLayer rootLayer = TransformLayer(transform: _rootTransform);
compiler message: ^^^^^^^^^^^^^^
compiler message: file:///Users/JohnnySmithh/Documents/flutter/packages/flutter/lib/src/rendering/view.dart:124:38: Error: Method not found: 'TransformLayer'.
compiler message: final ContainerLayer rootLayer = TransformLayer(transform: _rootTransform);
compiler message: ^^^^^^^^^^^^^^
compiler message: file:///Users/JohnnySmithh/Documents/flutter/packages/flutter/lib/src/rendering/view.dart:124:38: Error: The method 'TransformLayer' isn't defined for the class '#lib1::RenderView'.
compiler message: Try correcting the name to the name of an existing method, or defining a method named 'TransformLayer'.
compiler message: final ContainerLayer rootLayer = TransformLayer(transform: _rootTransform);
compiler message: ^
compiler message: file:///Users/JohnnySmithh/Documents/flutter/packages/flutter/lib/src/rendering/view.dart:125:15: Error: The method 'attach' isn't defined for the class 'invalid-type'.
compiler message: Try correcting the name to the name of an existing method, or defining a method named 'attach'.
compiler message: rootLayer.attach(this);
compiler message: ^
compiler message: file:///Users/JohnnySmithh/Documents/flutter/packages/flutter/lib/src/rendering/view.dart:195:13: Error: The method 'addToScene' isn't defined for the class 'invalid-type'.
compiler message: Try correcting the name to the name of an existing method, or defining a method named 'addToScene'.
compiler message: layer.addToScene(builder, Offset.zero);
compiler message: ^
compiler message: file:///Users/JohnnySmithh/Documents/flutter/packages/flutter/lib/src/rendering/view.dart:215:58: Error: The method 'find' isn't defined for the class 'invalid-type'.
compiler message: Try correcting the name to the name of an existing method, or defining a method named 'find'.
compiler message: final SystemUiOverlayStyle upperOverlayStyle = layer.find<SystemUiOverlayStyle>(top);
compiler message: ^
compiler message: file:///Users/JohnnySmithh/Documents/flutter/packages/flutter/lib/src/rendering/view.dart:220:35: Error: The method 'find' isn't defined for the class 'invalid-type'.
compiler message: Try correcting the name to the name of an existing method, or defining a method named 'find'.
compiler message: lowerOverlayStyle = layer.find<SystemUiOverlayStyle>(bottom);
compiler message: ^
Compiler failed on /Users/JohnnySmithh/IdeaProjects/MyCoolProject/lib/main.dart
Error launching application on iPhone X.
Exited (sigterm)
It's honestly a little hard to tell what's going on from just that error message. Do any errors show up in intellij before you build?
The only things I can think of are:
your flutter installation might be corrupted. You could try deleting and re-installing
your build directory might have something wrong. Try running flutter clean before building again
your libraries aren't being included properly. I'd assume this would show up in intellij, but maybe the paths are set differently there? Check Project Structure -> Libraries and see where the paths are set there.
(related to first) you might have multiple instances of flutter on your computer. If intellij is using one while the command line is using another that could potentially cause problems.
If your app happens to be open-source, or you're able to make a new app with the minimum amount of changes to reproduce the error, that would help significantly with diagnosing the problem =).

can't access cmake variable defined inside function using PARENT_SCOPE

So I have the following CMake code:
function(get_sources output_var)
set(${output_var} "a" PARENT_SCOPE)
message(${output_var}) # prints "SOURCES" (as expected)
message(${${output_var}}) # throws an error - why?
...
endfunction(get_sources)
get_sources(SOURCES)
message(${SOURCES}) # prints "a" - ok, so the above function created a var
The get_sources function is supposed to create a variable with given name in parent scope and fill it with some content.
It seems like get_sources(SOURCES) creates the variable as expected - message(${SOURCES}) outside function prints "a", but there's a problem with the line message(${${output_var}}). It should evaluate to message(a) and print "a", but instead it throws an error:
CMake Error at CMakeLists.txt:41 (message):
message called with incorrect number of arguments
I am confused. Is it supposed to be like that? Should I make a function scoped variable, fill it in and then at the end copy its contents to a parent scope variable? I checked it - it works, but I wanted it to be without any additional variables.
EDIT:
I enclosed a ${${output_var}} in quotation marks and now it doesn't throw an error, but it prints nothing.
PARENT_SCOPE sets variable only for parent scope, not for the current one. This is explicitely stated in CMake documentation about 'set' command:
If the PARENT_SCOPE option is given the variable will be set in the scope above the current scope. Each new directory or function creates a new scope. This command will set the value of a variable into the parent directory or calling function (whichever is applicable to the case at hand). The previous state of the variable’s value stays the same in the current scope (e.g., if it was undefined before, it is still undefined and if it had a value, it is still that value).
This is a proper way for handle PARENT_SCOPE variables:
Should I make a function scoped variable, fill it in and then at the end copy its contents to a parent scope variable?

Difference between dynamic and static type in Dart

Two questions.
First,
Below is strong type.
String msg = "Hello world.";
msg = "Hello world again.";
And, below dynamic
var msg = "Hello world.";
msg = "Hello world again.";
Is there any difference between the two 'msg's above?
Second, if I use 'new' keyword to initiate a variable as below,
Map myMap = new Map;
Why to indicate the variable 'myMap' is a Map instance(Map myMap) as 'new' keyword already include the same meaning? So, isn't it okay just,
myMap = new Map;
Because the 'new' keyword already implies the newly initiated variable is both variable and Map type, I can't understand why normally 'Map' keyword is with the variable name, even 'var' also.
Does anyone have any idea about this (seems a little bit redundant) Dart grammar?
In regard to the first question, there will be no difference in what each msg variable contains.
For the Map question, the reason for specifying the type of a variable that is constructed at declaration is to allow some flexibility with subclasses. Take for example the following code:
class SubMap extends Map {
SubMap() : super();
}
Map map = new SubMap();
Here we have a variable map which contains a SubMap object as its value, however we are allowing it to contain values of type Map (or other types which subclass Map) at later times.
The main thing to remember with Dart is that it is optionally typed. When running your code, none of your type annotiations make any difference at all (unless you run in checked mode). What the type annotations are for is to help your IDE and other tools provide autocomplete help, possible warnings, etc. which other fully dynamic languages like Javascript cannot.
String msg = "Hello world.";
msg = "Hello world again.";
msg = 1; // exception in checked mode - int can not be assigned to String.
var msg = "Hello world.";
msg = "Hello world again.";
msg = 1; // ok in checked mode
Checked mode is the developer mode where type annotations are checked and create runtime exceptions when code violates them.
In unchecked (production) mode it makes no difference if you add a type annotation and which one. This is for performance reasons because checked mode is slower.
The declaration Map myMap = new Map() does three things:
it declares a variable named myMap with type-annotation Map,
it creates a new object using the Map constructor, and
it assigns the object to the variable.
The type annotation means that myMap has static type Map. The static type can give you some warnings at compile time, but it doesn't change anything at runtime.
The type annotation also adds a type check in checked mode, where any assignment to the variable is checked for being assignable to Map.
The object you create could be any object using any constructor. I regularly use new HashMap() instead of new Map(), and still assign it to a variable typed as Map.
Then the object is assigned to the variable, and the type check succeeds, if in checked mode.
The variable is independent of the value used to initialize it. If you later assign a string to the myMap variable, it will fail in checked mode.
So, the difference between the two msg variables is that one has a static type and a type check in checked mode.
For the code you show, there is no difference in behavior.
If the next line was var y = msg + 42;, then the typed version would give a warning (msg has static type String, and String.operator+ has type String->String, so the 42 has an invalid argument type), where the untyped version wouldn't warn you (msg has type dynamic so you probably know what you are doing).

How to dynamically call a method in python?

I would like to call an object method dynamically.
The variable "MethodWanted" contains the method I want to execute, the variable "ObjectToApply" contains the object.
My code so far is:
MethodWanted=".children()"
print eval(str(ObjectToApply)+MethodWanted)
But I get the following error:
exception executing script
File "<string>", line 1
<pos 164243664 childIndex: 6 lvl: 5>.children()
^
SyntaxError: invalid syntax
I also tried without str() wrapping the object, but then I get a "cant use + with str and object types" error.
When not dynamically, I can just execute this code to get the desired result:
ObjectToApply.children()
How to do that dynamically?
Methods are just attributes, so use getattr() to retrieve one dynamically:
MethodWanted = 'children'
getattr(ObjectToApply, MethodWanted)()
Note that the method name is children, not .children(). Don't confuse syntax with the name here. getattr() returns just the method object, you still need to call it (jusing ()).