Dart Editor: "unused import" errors - dart

For a project, I'm importing a library but I don't use any of the classes in it directly. The goal is to fetch a ClassMirror at runtime to create an instance from it. So I have something like:
import 'controllers.dart';
main() {
ClassMirror controller = getClassFromString(libraryName: 'deck_app', className: 'HomeController');
InstanceMirror instance = controller.newInstance(new Symbol(''), []);
instance.reflectee.sayHey();
}
This gives me an "unused import" error. Idk if this is to be considered a bug. So I'm asking you: do you think this is to be considered as a bug? If not, is there a way I could suppress unused import errors?
What's weird is I thought Dart would tree-shake the source and remove the unused import's code, but it does not. The library is properly imported and available.

The unused import is just a conclusion from the static analyzer. You can ignore it or add a dummy statement to silence the analyzer. This has no effect when you run the application.

Related

Using java in rascal

I have made a java function genImage(List<String lines) which is located in class vis. I am trying to import it into my rascal code, but it won't work. This is the last of my efforts to import it:
#javaClass{visualization.vis}
java void genImage(list[str] lines);
The error I get:
Cannot link method visualization.vis because: visualization.vis.(io.usethesource.vallang.IValueFactory)
Advice: |http://tutor.rascal-mpl.org/Errors/Static/JavaMethodLink/JavaMethodLink.html%7C
The #javaClass tag must point to a fully qualified classname, including the package and the class. It seems it's the class you are missing, right?

Define const level class in Flutter

I want define a class Const to reuse and easier to handle edit, manternance. Example:
My issue screenshot
But Dart Analysis has 2 warnings such as:
error: The const variable 'String' must be initialized.
error: Expected to find ';'.
There's isn't any line #5 in your class, neither any String variable. Try refreshing your IDE
Perhaps the log you are getting is from some previous build!

Ambiguous type name in Swift unit test

Recently I spent quite some time to figure it out the issue with my unit test. The error that I saw in my unit test is :
Ambiguous type name 'AssetIdentifier' in 'UIImage'
This is the complete code :
import XCTest
import module
#testable import module
class VoucherOptionsViewControllerTests: XCTestCase {
lazy var result = ""
class VoucherOptionsViewControllerMock: VoucherOptionsViewController {
var result = ""
override func showOverlayView(title: String, message: String, assetIdentifier: UIImage.AssetIdentifier) {
self.result = "lalalala"
}
}
}
AssetIdentifier is an enum in UIImage extension
I tried the suggestion listed in:
Ambiguous type name error
However, it returns me with a different error on the controller:
'AssetIdentifier' is inaccessible due to 'internal' protection level
I've tried to clean and rebuild but it returns the same error.
I think your issue lays in the double import of the module module.
import module
#testable import module
By importing it twice you give the compiler two (identical) versions of every type and function defined in module. That's why it's complaining about the ambiguity of AssetIdentifier, there's two of those and it doesn't know which to pick.
The right thing to do would be to remove import module though.
The #testable import statement allows you to import a module in your test target and to gain access to the types and functions defined internal as well as public.
When you add the #testable attribute to an import statement for a module compiled with testing enabled, you activate the elevated access for that module in that scope. Classes and class members marked as internal or public behave as if they were marked open. Other entities marked as internal act as if they were declared public.
Source.
If you do that you'll see that you won't have ambiguity issues and you won't need to move the UIImage extension in the test target. As #JonReid rightfully pointed out in his comment "Production code should remain in your production target".
Hope this helps.
Although that thread does not give the real answer, it gives great clue about the issue. So the fix that I found is by removing :
#testable import module
I keep the UIImage extension in the test module so the compiler does not complain.

Get warnings when programmatically parsing Dart file with analyzer_experimental

I am using analyzer_experimental to parse a Dart file into a CompilationUnit:
import 'package:analyzer_experimental/analyzer.dart';
var unit;
try {
unit = parseDartFile(path);
} on AnalyzerErrorGroup catch(e){
print(e);
}
The above code will catch any parsing errors encountered.
I am also interested in seeing any warnings associated with the file (e.g. 'Undefined name "foo"'). I know that the experimental_analyzer library has the capability to generate these warnings when running from the command line but it does not seem to be possible to get the warnings programmatically, without directly referencing classes in the src folder (which seems like a bad idea).
Is there any way to achieve this?
It's likely this package was very incomplete at the time.
There's now an analyzer package on pub and also a (work-in-progress) STDIN/STDOUT Analyzer Service aimed to help making tooling support easier for IDE extension authors.

Scala error compiling OptionBuilder

I am using Apache commons cli (1.2) for command line parsing.
I have the following in my code:
import org.apache.commons.cli.OptionBuilder
OptionBuilder.withLongOpt("db-host").hasArg.
withDescription("Name of the database host").create('h')
I get the error hasArg is not a member of org.apache.commons.cli.OptionBuilder. It makes no difference if I change .hasArg to .hasArg().
Why?
BTW, Java parses this fine.
import org.apache.commons.cli.OptionBuilder
OptionBuilder.withLongOpt("db-host").hasArg.
withDescription("Name of the database host").create('h')
I get the error hasArg is not a member of org.apache.commons.cli.OptionBuilder. It makes no difference if I change .hasArg to .hasArg().
Why?
Because there is no instance method hasArg in OptionBuilder, only a static method. Since hasArg is a static method, you obviously need to call it on the class, not on an instance of the class.
BTW, Java parses this fine.
I don't understand what this has to do with parsing. Scala parses this just fine, too. Plus, what some completely different programming does or doesn't do with this code is utterly irrelevant, since this is Scala code, not some other language.
You need to do something like this:
import org.apache.commons.cli.OptionBuilder
OptionBuilder.withLongOpt("db-host")
OptionBuilder.hasArg
OptionBuilder.withDescription("Name of the database host")
val optionParser = OptionBuilder.create('h')

Resources