What´s the difference of using hidden var or static constant??
What´s the difference of using hidden var or static constant??
A const cannot be bound to another object after it has been initialized. A var can be re-bound.
class MyClass { var _myVar = 1; const _myConst = 2; function initialize() { _myVar = 3; // legal _myConst = 4; // illegal; symbol not found } }
The const modifier in MonkeyC is not really const as it is other languages. It is more like Java's final. If the thing that is const is an object type you can still modify the object itself, you just can't change which object the const value references.
class MyClass { const _myConst = {}; function initialize() { AppBase.initialize(); _myConst.put(1, 1); // legal _myConst = {}; // illegal; symbol not found } }
The hidden keyword is an access specifier. It, and its synonym protected, are like the protected keyword in C++. It tells the compiler to allow access to the variable from the class that it is declared in, and in derived classes. There are also private and public access specifiers. The former only allows access to the class it is declared in (not derived classes) and public allows access to anyone and everything.
Finally, the static keyword is a storage specifier that tells the compiler to generate just one variable that is shared among all instances of a class.
class MyClass { static var _mySharedVar = 1; function initialize() { } } // somewhere else... function test() { var c1 = new MyClass(); var c2 = new MyClass(); System.println(c1._mySharedVar); // 1 System.println(c2._mySharedVar); // 1 c1._mySharedVar = 2; System.println(c1._mySharedVar); // 4 System.println(c2._mySharedVar); // 4 }
Since a static variable isn't part of the object (_mySharedVar is not part of c1 or c2, it is party of the type MyClass), you can reference the variable without an object instance like this..
MyClass::_mySharedVar = 9; // modifies the shared value
You can mix and match const/var with access specifiers (public/protected/hidden/private) and storage specifiers in many ways. For example:
// only accessed by this class and derived classes // only one copy of the value is shared // cannot be modified hidden static const a = 1; // accessed by anyone // one value per class instance // can be modified public var b = 2;
Thanks a lot Travis very useful information!!
Hi,
am I correct to assume, that it is not possible to do check during app run-time whether some class instance has some constant?
"classInstance has CONST" doesn't work.
Seems to work just fine for me.
using Toybox.Application; using Toybox.WatchUi; class classA { } class classB { const CONST = 1.0; } class TestApp extends Application.AppBase { function initialize() { AppBase.initialize(); } function getInitialView() { if (classA has :CONST) { System.println("classA::CONST exists"); } else { System.println("classA::CONST does not exist"); } if (classB has :CONST) { System.println("classB::CONST exists"); } else { System.println("classB::CONST does not exist"); } var objA = new classA(); if (objA has :CONST) { System.println("objA::CONST exists"); } else { System.println("objA::CONST does not exist"); } var objB = new classB(); if (objB has :CONST) { System.println("objB::CONST exists"); } else { System.println("objB::CONST does not exist"); } return [ new WatchUi.View() ]; } }
classA::CONST does not exist classB::CONST exists objA::CONST does not exist objB::CONST exists
Thanks Travis! Didn't try to write const as symbol.