You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@royale.apache.org by GitBox <gi...@apache.org> on 2019/04/11 22:58:29 UTC

[GitHub] [royale-compiler] joshtynjala opened a new issue #82: Private constructors in ActionScript

joshtynjala opened a new issue #82: Private constructors in ActionScript
URL: https://github.com/apache/royale-compiler/issues/82
 
 
   ## Proposed syntax
   
   For singletons:
   
   ``` actionscript
   package com.example
   {
   	public class MySingletonClass
   	{
   		private static var _instance:MySingletonClass;
   
   		public static function get instance():MySingletonClass
   		{
   			if(!_instance)
   			{
   				_instance = new MySingletonClass();
   			}
   			return _instance;
   		}
   
   		private function MySingletonClass()
   		{
   			
   		}
   	}
   }
   ```
   
   Also useful for classes that are meant to provide static methods only:
   
   ``` actionscript
   package com.example
   {
   	public class MyUtils
   	{
   		public static function utilityFunction1():void {}
   		public static function utilityFunction2():void {}
   		public static function utilityFunction3():void {}
   
   		private function MyUtils() {}
   	}
   }
   ```
   
   ## Semantics
   
   At compile time, use of a class with a private constructor in a `new` statement should result in an error, if it is in another class.
   
   ``` actionscript
   new MySingletonClass(); //error in another class
   ```
   
   Similar to the `InaccessibleMethodReferenceProblem` that is created when trying to call a method in the wrong scope, there should be a new problem for private constructors with this message:
   
   > Attempted access of inaccessible constructor through a reference with static type MySingletonClass
   
   When this feature is enabled, an alternative to `ConstructorMustBePublicProblem` should be mention `private` too:
   
   > A constructor can only be declared public or private
   
   ## How to enable
   
   New ActionScript syntax should be opt-in because IDEs and other tooling may use the Apache Royale compiler with other SDKs that don't support the syntax. Developers can enable private constructors using a new `allow-private-constructors` compiler option.
   
   ``` sh
   mxmlc -allow-private-constructors App.mxml
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services