shadowing

immutable in F#

喜欢而已 提交于 2019-12-04 00:55:41
问题 I know that variables in F# are immutable by default. But, for example in F# interactive: > let x = 4;; val x : int = 4 > let x = 5;; val x : int = 5 > x;; val it : int = 5 > So, I assign 4 to x, then 5 to x and it's changing. Is it correct? Should it give some error or warning? Or I just don't understand how it works? 回答1: When you write let x = 3 , you are binding the identifier x to the value 3 . If you do that a second time in the same scope, you are declaring a new identifier that hides

Shadowing instance variables with local variables in Java

断了今生、忘了曾经 提交于 2019-12-02 22:24:18
问题 I have read that " a variable is shadowed if there is another variable with the same name that is closer in scope". I found this Point class with a constructor as an example: public class Point { public int x = 0; public int y = 0; public Point(int x, int y) { x = x; y = y; } } Then I created an object of the Point class in the CreateObjectDemo class below and printed the value of the variable x. public class CreateObjectDemo { public static void main(String[] args) { Point originOne = new

Why is the output different in the two cases?

假如想象 提交于 2019-12-02 01:59:44
问题 Why is the output different in the below case even when, the variable has been overridden? public class A { int a = 500; void get() { System.out.println("a is " + this.a); } } public class B extends A { int a = 144; } public class mainmethod { public static void main(String args[]) { B ob = new B(); System.out.println("a is " + ob.a); ob.get(); } } 回答1: There is no such thing as overridden variables. B actually has two instance variables named a : one it declares and another it inherits. See

Why is the output different in the two cases?

蹲街弑〆低调 提交于 2019-12-02 00:27:32
Why is the output different in the below case even when, the variable has been overridden? public class A { int a = 500; void get() { System.out.println("a is " + this.a); } } public class B extends A { int a = 144; } public class mainmethod { public static void main(String args[]) { B ob = new B(); System.out.println("a is " + ob.a); ob.get(); } } There is no such thing as overridden variables. B actually has two instance variables named a : one it declares and another it inherits. See this: B ob = new B(); System.out.println("B.a is " + ob.a); System.out.println("A.a is " + ((A)ob).a);

access to shadowed variable in local class

梦想的初衷 提交于 2019-12-01 21:52:35
问题 i'm new in java and i confused for below example public class Test { int testOne(){ //member method int x=5; class inTest // local class in member method { void inTestOne(int x){ System.out.print("x is "+x); // System.out.print("this.x is "+this.x); } } inTest ins=new inTest(); // create an instance of inTest local class (inner class) ins.inTestOne(10); return 0; } public static void main(String[] args) { Test obj = new Test(); obj.testOne(); } } why i can't access to shadowed variable in

Derived class not inheriting overloaded method from base class

不问归期 提交于 2019-12-01 20:29:16
问题 I would like to have a method in a base class call a pure virtual method that will be implemented in a derived class. However, the base class parameterless method doesn't seem to be inherited by the derived class. What am I doing wrong? Compiler is MSVC12. error C2660: 'Derived::load' : function does not take 0 arguments Here is a complete example (that doesn't compile due to the error): struct Base { void load() { load(42); }; // Making this virtual doesn't matter. virtual void load(int i) =

Can a parameter of a template template parameter cause shadowing?

三世轮回 提交于 2019-12-01 17:42:08
Is this legal C++? template <typename T, template <typename T> class> struct S { }; Clang (3.7.1) rejects it, complaining the second T shadows the first T . GCC seems not to care about it and I think that's reasonable. I think it is only the number of parameters that matters in a template template parameter. http://goo.gl/51bHVG (gcc.godbolt.org) No. [temp.local]/6 : A template-parameter shall not be redeclared within its scope (including nested scopes). 来源: https://stackoverflow.com/questions/35744299/can-a-parameter-of-a-template-template-parameter-cause-shadowing

Is there a way to have warnings for shadowing values in F# in Visual Studio?

落花浮王杯 提交于 2019-12-01 16:39:41
To me shadowing of existing values like described in: Shadowing and Nested function immutable in F# f# duplicate definition FSharp for fun and profit comment seems to be going against the notion of immutability and type safety that makes F# so strong. Shadowing in F# works different than in C#. It just took me quite some time to find out that a bug in my code was due to unintentional shadowing of a name within the same scope. Is there a way to have compiler warnings for shadowing values in VS? I know that in some cases it can be useful. for example for Checked Aritmetics . Shadowing has pros

Can a parameter of a template template parameter cause shadowing?

人走茶凉 提交于 2019-12-01 15:43:28
问题 Is this legal C++? template <typename T, template <typename T> class> struct S { }; Clang (3.7.1) rejects it, complaining the second T shadows the first T . GCC seems not to care about it and I think that's reasonable. I think it is only the number of parameters that matters in a template template parameter. http://goo.gl/51bHVG (gcc.godbolt.org) 回答1: No. [temp.local]/6: A template-parameter shall not be redeclared within its scope (including nested scopes). 来源: https://stackoverflow.com

f# duplicate definition

会有一股神秘感。 提交于 2019-12-01 02:20:47
问题 in F# powerpack math provider source code: I saw this (in lapack_service_netlib.fs ) member this.dgemm_((a:matrix),(b:matrix)) = // allocate results let c = Matrix.zero (m) (n) // transpose let c = Matrix.transpose c ... // fixups let c = Matrix.transpose c // result tuple c Why does this complile? does c get duplicate definition? 回答1: This is shadowing ; at function/class/member scope, any local let bindings will be shadowed by subsequent let bindings to the same name. See also Shadowing and