Can't access to a private method in Dart Library - dart

I have to access to a private method in a Class. I created a library with inside the Class, imported it but it can't still recognize. I have the same problem also with private variables.
Example:
file buffer.dart:
library buflib;
class Buffer{
void _record(){
[...]
}
}
in the same folder: engine.dart
import 'buffer.dart';
class Engine {
Buffer _buff = Buffer()
[...]
void myMethod(){
[...]
this._buff._record();
}
}
I have this error:
The method '_record' isn't defined for the type 'Buffer'.
Try correcting the name to the name of an existing method, or defining >a method named '_record'.dartundefined_method
Any suggestions?

By default, each separate .dart file is a separate library. Since private identifiers are private to the library, they won't be visible to other .dart files.
You can use the library and part of directives to group multiple .dart files into the same library, but those directives aren't documented.

u cant call private methods or variables from out there classes .... u should change it to public by removing "under score"

Related

Should subclasses inherit private mixin variables in Dart?

Should I get the following error:
class.dart:11:11: Error: The getter '_privateID' isn't defined for the class 'Y'.
- 'Y' is from 'class.dart'.
Try correcting the name to the name of an existing getter, or defining a getter or field named '_privateID'.
From the following code?
mixin.dart:
class Mixin {
static int _nextID = 0;
int publicID = _nextID++; // I only need one of these lines
int _privateID = _nextID++; // but this variable is inaccessible
}
class.dart:
import 'mixin.dart';
class X with Mixin {
void run() {
print(publicID); // no error here
}
}
class Y with Mixin {
void run() {
print(_privateID); // Error: _privateID not defined
}
}
void main() {
Y().run();
}
Or is this a bug? If it's not a bug, I'd like to understand why this behavior is reasonable.
When I instead define the mixin in the same file as the above classes, I get no error.
(Dart SDK 2.4.1.)
It is not a bug.
The private field is inherited, but you cannot access it because its name is private to a different library.
Dart's notion of "privacy" is library private names.
The name _privateID in the mixin.dart library introduces a library private name. This name is special in that it can only be written inside the same library.
If someone writes _privateID in a different library, it is a different name, one unique to that library instead.
It is as if private names includes the library URI of the library it is written in, so what you really declare is a name _privateID#mixin.dart.
When you try to read that field in class.dart, you write ._privateID, but because it is in a different library, what you really write is ._privateID#class.dart, a completely different name, and the classs does not have any declarations with that name.
So, if one class needs to access a private member of another class (or mixin, or anything), then the two needs to be declared in the same library, because otherwise they cannot even write the name of that variable.
That is why the code works if you write the mixin in the same library.
If you want to move the mixin to a separate file, but not necessarily a separate library, you can use a part file.

how possible to override public init(frame:) within an external module? [duplicate]

Swift 3 introduced the new open keyword that I'm using in a framework.
Does an open class in this framework require an open initialiser to be used outside of said framework, or does the init function inherit the open declaration on the class?
For example:
open class OpenClass {
var A: String
init() { // does this init() function need to be marked open?
A = String()
}
}
Side question: do the variables in the open class OpenClass inherit the open nature of their class?
From SE-0117 Allow distinguishing between public access and public overridability:
Initializers do not participate in open checking; they cannot be declared open, and there are no restrictions on providing an initializer that has the same signature as an initializer in the superclass.
You need not and you cannot declare a init method as open:
open class OpenClass {
open init() { // error: only classes and overridable class members can be declared 'open'; use 'public'
}
}
The default access level for all members of a class (properties
and methods) is internal, that applies to open classes as well.

How to get my own ICultureSelector implementation injected in the framework

I have implemented my version of ICultureSelector, in a custom module.
Here it is a part of its definition (my question is not about the logic to select the culture; I tried with my own namespace and also with a namespace same as the namespace used in the module Orchard.Localization):
namespace Orchard.Localization.Selectors
{
[OrchardFeature("Orchard.Localization.CultureSelector")]
public class ShortRouteCultureSelector : ICultureSelector
{
public CultureSelectorResult GetCulture(HttpContextBase context)
{
...
I put a breakpoint in the method GetCurrentCulture of the class CurrentCultureWorkContext in the Orchard.Framework project, and I see that the variable IEnumerable _cultureSelectors contains all the implementation of ICultureSelector of the module Orchard.Localization but not my implementation, that is never used.
What am I missing?
Remove the OrchardFeature attribute or define your own feature name as Orchard.Localization.CultureSelector is defined in Orchard.Localization module already.

access configuration/property files from src/groovy

I have a file under src/groovy and I have some properties that are in my Config.groovy and in external property file too. Normally if one want access properties its possible to use grailsApplication .configuration.property.name expression. I want to be able to access all those properties from this file that is under src/groovy directory. What I've tried so far
import grails.util.Holders
class ForkedTomcatCustomizer {
def application
void customize(Tomcat tomcat) {
println Holders.grailsApplication.config.property.name
}
}
gave me NPE saying that grailsAppliction is null
import org.codehaus.groovy.grails.web.context.ServletContextHolder as SCH
import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes as GA
class ForkedTomcatCustomizer {
def application
void customize(Tomcat tomcat) {
def ctx = SCH.servletContext.getAttribute(GA.APPLICATION_CONTEXT)
def grailsAppliction = ctx.grailsApplication.getObject()
println grailsAppliction.config.property.name
}
}
the same - NPE because grailsAppliction is null
Is it possible to handle this situation somehow? Thank you!
Use the below and see if it works
println Holders.config.property.name
You don't need grailsApplication when using Holders.
The examples below are probably a little more complex than what you need, but they show how to get a configuration property at build time. I use them to merge two configuration files, but you might not need to do that.
This method returns a config property when called here at the CompileEnd event.
You could define a similar method in your app's _Events.groovy file that calls your own configuration holder class.
import org.codehaus.groovy.grails.commons.ConfigurationHolder;
class KeyAndSecret{
public static String consumerKey = ConfigurationHolder.config.consumerKey;
public static String consumerSecret = ConfigurationHolder.config.consumerSecret;
}
Try like this

How to use the [mixin] tag in AS3 applications?

I have the following two projects in in Flex Builder 3:
One AS3 library project (generates a SWC file)
One Flex application project (MXML Application)
The MXML Application references to the AS3 library project (Flex build path). So far, so good. I now want to run code automatically when an application uses the AS3 library. The [mixin] tag should do exactly what I need.
I followed the instructions from http://nondocs.blogspot.com/2007/04/metadatamixin.html and checked out the AutoQuick project. The latter is an example project by Adobe showing the use of the automation framework. In this project they are using the [mixin] tag (class AQAdapter).
I followed the examples but my code is not working. The static init method is not called. I added the library to the compiler arguments list that didn't work either.
How do I get this to work?
/* class to be automatically loaded */
package {
/* includes */
[mixin]
public class TestApp extends Sprite {
/* additional members */
private static var mContainer:DisplayObjectContainer;
private static var mInstance:TestApp;
/**
* #private
*/
public static function init(root:DisplayObject):void
{
if(!mInstance)
{
mContainer = root as DisplayObjectContainer;
mContainer.addEventListener(FlexEvent.APPLICATION_COMPLETE, applicationCompleteHandler);
}
}
}
}
With the [Mixin] tag, the static init() method will be called at application start-up, as long as the class is referenced directly or indirectly from the main application.
Also, you have to remember that this method is run in a static context, so you shouldn't reference methods or attributes that require an instance (non-static), without creating the instance first.
Link: http://adamflater.blogspot.com/2007/03/static-code-blocks.html

Resources