Debugging with extension methods seems to be a bit problematic currently.
Consider the following code:
import 'dart:developer';
void main() {
final a = A();
workaround = a.extMethod;
debugger();
a.normalMethod();
a.extMethod();
}
class A {
String normalMethod() => 'hello from class';
}
extension AX on A {
String extMethod() => 'hello from extension';
}
When debugging this, stepping into both methods (normalMethod() and extMethod()) will work fine, the only difference is that this isn't available for extMethod() for some reason (there is #this instead, but it can't be used anyhow).
The extension method (extMethod()) is impossible to use for evaluations in the debug console, though.
a.normalMethod()
"hello from class"
a.extMethod()
Unhandled exception:
NoSuchMethodError: Class 'A' has no instance method 'extMethod'.
Receiver: Instance of 'A'
Tried calling: extMethod()
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:38:5)
#1 Eval ()
workaround()
"hello from extension"
I have found kind of a workaround as you can see, but it has to be in place before running the program so I'd definitely prefer a better method that can be used on fly without any preparation.
I have found two very old issues describing this exact problem but both are closed and from what I understand, unresolved.
https://github.com/dart-lang/sdk/issues/44801
https://github.com/dart-lang/sdk/issues/44472
I'm using Dart 2.18.6 (latest stable).
Related
I'm trying to play with xhp and I'm finding that running the one xhp example I could find https://github.com/hhvm/xhp-js-example is issuing an error \nFatal error: Uncaught Error: Found top-level code in :\nStack trace:\n#0 {main} when following the README as is on HHVM 4.71. Removing require_once(__DIR__.'/vendor/autoload.php'); resolves the top-level code error but I'm now stuck with the error \nFatal error: Uncaught Error: unknown class xhp_x__element in :\nStack trace:\n#0 /Users/user/code/xhp-js-example/example.php(12): <unknown>()\n#1 {main}. I've tried to change the code in example.php to the one found here:
class :introduction extends :x:element {
protected function render(): \XHPRoot {
return <strong>Hello!</strong>;
}
}
class :intro-plain-str extends :x:element {
protected function render(): \XHPRoot {
// Since this function returns an XHPRoot, if you want to return a primitive
// like a string, wrap it around <x:frag>
return <x:frag>Hello!</x:frag>;
}
}
echo <introduction />;
echo PHP_EOL.PHP_EOL;
echo <intro-plain-str />;
since that didn't work, I also tried the simple example from here:
<?hh // strict
$user_name = 'Fred';
echo <tt>Hello <strong>{$user_name}</strong></tt>;
but altered to resolve the top-level error by wrapping it in a function and annotating it as follows:
<?hh // strict
<<__EntryPoint>>
function main(): void {
$user_name = 'Fred';
echo <tt>Hello <strong>{$user_name}</strong></tt>;
}
I'd get a very similar error \nFatal error: Uncaught Error: Class undefined: xhp_tt in /Users/user/code/xhp-js-example/example.php:6\nStack trace:\n#0 (): main()\n#1 {main}
Any help with getting these seemingly simple examples to work would be much appreciated since it's very painful and discouraging for trying this tech when the basic examples don't seem to run. this is using the macos package of hhvm on Catalina.
EDIT: I've also tried this without the use of xhp-js, which is included in the example repo and am getting the same error message.
There are a few roadblocks that the OP encountered to get XHP running with HHVM 4.62, as discussed in the comments.
xhp-js and xhp-js-example are both outdated by a few years so they can't compile due to breaking changes to HHVM itself, as the OP saw.
While the XHP syntax is built into Hack, all the standard implementations are provided by xhp-lib, so it is necessary to autoload that in order to use the standard library of tags and tag classes.
New to HHVM 4.62 is mandatory FIXME whitelisting, which requires package writers to explicitly specify allowed HH_FIXME codes in the .hhconfig. These were added to XHP-lib in 4.0.0rc1, so this version is necessary when running on HHVM 4.62.
Therefore, a minimal project with XHP on 4.62 looks like this:
composer.json
{
"require": {
"hhvm": "~4.62",
"hhvm/hhvm-autoload": "^3.1.3",
"facebook/xhp-lib": "~4.0.0rc1"
}
}
hh_autoload.json
{ "roots": [ "src/" ] }
src/example.hack
require_once(__DIR__ . "/../vendor/hh_autoload.hh");
<<__EntryPoint>>
function main(): void {
echo <div>{1 + 2}</div>;
}
The assert statement method only works in development. Is there a standard Dart function which works like assert but which will be executed when running production code?
I'm looking for something like this:
void myFunc(List myList) {
validate(myList.isNotEmpty);
...
}
There is nothing in the Dart SDK, but Google's quiver package provides a checkState function that does what you want.
In dart, when developing a web application, if I invoke a method with a wrong number of arguments, the editor shows a warning message, the javascript compilation however runs successfully, and an error is only raised runtime. This is also the case for example if I refer and unexistent variable, or I pass a method argument of the wrong type.
I ask, if the editor already know that things won't work, why is the compilation successful? Why do we have types if they are not checked at compile time? I guess this behaviour has a reason, but I couldn't find it explained anywhere.
In Dart, many programming errors are warnings.
This is for two reasons.
The primary reason is that it allows you to run your program while you are developing it. If some of your code isn't complete yet, or it's only half refactored and still uses the old variable names, you can still test the other half. If you weren't allowed to run the program before it was perfect, that would not be possible.
The other reason is that warnings represent only static type checking, which doesn't know everything about your program, It might be that your program will work, it's just impossible for the analyser to determine.
Example:
class C {
int foo(int x) => x;
}
class D implements C {
num foo(num x, [num defaultValue]) => x == null ? defaultValue : x;
}
void bar(C c) => print(c.foo(4.1, 42)); // Static warning: wrong argument count, bad type.
main() { bar(new D()); } // Program runs fine.
If your program works, it shouldn't be stopped by a pedantic analyser that only knows half the truth. You should still look at the warnings, and consider whether there is something to worry about, but it is perfectly fine to decide that you actually know better than the compiler.
There is no compilation stage. What you see is warning based on type. For example:
This code will have warning:
void main() {
var foo = "";
foo.baz();
}
but this one won't:
void main() {
var foo;
foo.baz();
}
because code analyzer cant deduct the type of foo
I'm currently calling a jQuery based plugin called Bootstrap Context Menu.
In order to call it, I need to use the Javascript Interop library. But when I call a jQuery method from it I receive the following warning:
The method 'jQuery' is not defined for the class 'Proxy'
Code snippet:
js.scoped(() {
js.context.jQuery('#canvas').contextmenu();
});
This was not happening before some dart/js-interop updates. What is the right way to get rid of this warning?
You get this warning because the new analyzer doesn't seem to be aware of the option Report 'no such member' warnings when class defines noSuchMethod() ( Reported at http://dartbug.com/10016 ). If you switch back to the legacy analyzer you shouldn't see this warning anymore.
That said if you want to use the new analyzer and get rid of this warning you can use the array notation like this :
js.context["jQuery"]('#canvas')["contextmenu"]();
But :
it's less readable particullary for method calls.
it's less efficient for method calls because 2 operations are done ( f = js.context["jQuery"] followed by f('#canvas') ) instead of 1 ( js.context.jQuery('#canvas') )
I've created a code sample that shows the issue I'm having:
class BindingExample {
public static void main(String[] args) {
Closure closure1 = {
printit.call("Hello from closure 1")
}
Closure closure2 = {
printit("Hello from closure 2")
}
Closure printit = { s ->
println("printing: "+s)
}
Binding binding = new Binding()
binding.setVariable("printit", printit)
closure1.delegate = binding
closure2.delegate = binding
closure1() //This works fine
closure2() //This does not.
//Why does .call() work and () alone not? Most documentation says they're the same.
}
}
Printit is a Closure, which the documentation indicates implements doCall and therefore is callable in short form via ().
However, when this closure is made available via binding to a delegate, only the long-form version of the call is permitted. The output is:
printing: Hello from closure 1
Exception in thread "main" groovy.lang.MissingMethodException: No signature of method: groovy.lang.Binding.printit() is applicable for argument types: (java.lang.String) values: [Hello from closure 2]
Can someone explain why this is the case? If possible, I'd like to also see how to make it so the short-form version works. I was able to make it work by defining printit as a proper static method (not a closure), but that won't work for my case because I actually need printit to be given some data available only inside of the method scope (not included in the example since my question relates to the binding itself).
As to WHY this is the case, I can't give a definite answer, unfortunately. There's some talk about implicit-"this" annotation, etc. It seems like it should work, but that there's some vagueness about what should be tried first (this-scope or delegate).
That the issue exists, currently, seems correct. I've found the following other resources that agree, with some discussion without resolution about why.
Nabble discussion about the issue:
http://groovy.329449.n5.nabble.com/Binding-Closure-property-not-called-as-method-td5562137.html
JIRA ticket resulting:
https://issues.apache.org/jira/browse/GROOVY-5367