Vala: calling super class creation method at constructor - vala

I have been trying to initialize a parent class using it's creation method.
class A {
public A.creator (int x, int y) {
// do some magic
}
}
class B : A {
public B.creator (int x, int y) {
// I want to do something like
base.creator (x, y);
}
}
I am facing an error while trying to run the above code.
error: chain up to 'A.creator' not supported
What's the correct way to accomplish this in vala?

When I try to compile your code I get:
chain.vala:1.1-1.7: error: Class name `A' is too short
class A {
^^^^^^^
chain.vala:7.1-7.11: error: Class name `B' is too short
class B : A {
^^^^^^^^^^^
Compilation failed: 2 error(s), 0 warning(s)
After renaming A to Aaa and B to Bbb the code compiles just fine with valac 0.36.15.
My first thought was that you might have to derive A from Object, but apparently that is not the case.

Related

Compilation with module refinement

When compiling the following code:
module Interface {
function addSome(n: nat): nat
ensures addSome(n) > n
}
module Mod {
import A : Interface
method m() {
assert 6 <= A.addSome(5);
print "Test\n";
}
}
module Implementation refines Interface {
function addSome(n: nat): nat
ensures addSome(n) == n + 1
{
n + 1
}
}
module Mod2 refines Mod {
import A = Implementation
}
method Main() {
Mod2.m();
}
I get the output
Dafny program verifier finished with 5 verified, 0 errors
Compilation error: Function _0_Interface_Compile._default.addSome has no body
Given that Implementation refines Interface, why does the compiler need Interface.addSome to have a body, particularly when addSome is ghost anyway so shouldn't be involved in compilation?
You need to mark both Interface and Mod as abstract. Among other things, this means they will not be compiled, so you won't get that error.
After those two small changes, the rest of your file compiles correctly.

Does FutureOr<T> have a reified type of Future<T> / <T>?

I have a class in AngularDart as followings:
abstract class Validator {
Map validate(AbstractControl c);
}
Looking closely, this used to be (before we added strong-mode support):
abstract class Validator {
validate(AbstractControl c);
}
The issue that it technically supports returning a Future or Map.
I'd like to refactor this and properly type it using FutureOr:
abstract class Validator {
FutureOr<T> validate(AbstractControl c);
}
Will I be able to use an is check at runtime? (In DDC and dart2js)
void runValidator(Validator v) {
var result = v.validate(...);
if (result is Future) {
// async...
} else {
// sync...
}
}
Am I thinking about this correctly?
EDIT: As mentioned below, I mean
if (result is Future<T>) {
} else if (result is T) {
}
One more question, would validate match these two typedefs:
Future<Map> AsyncValidate(AbstractControl c);
Map SyncValidate(AbstractControl c);
Yes, you can do result is Future<Map>. The actual value returned by the validate method is either a Future or it's not. The static type of the function doesn't affect that, and since FutureOr<Map> isn't an actual class, you can't have an object that is "a FutureOr". It's either a real Future<Map> or it's a Map.
For the second question, that depends on what yo mean by "match".
You can override the method with a method that returns either Map or FutureMap:
abstract class Validator {
FutureOr<Map> validate(abstractControl c);
}
class AsyncValidator extends Validator {
Future<Map> validate(AbstractControl c) {...}
}
class SyncValidator extends Validator {
Map validate(AbstractControl c) {...}
}
That is, you can use one of the function types you mention as a Validator.validate, but not in the other direction.
typedef FutureOr<Map> Validate(AbstractControl c);
typedef Future<Map> AsyncValidate(AbstractControl c);
typedef Map SyncValidate(AbstractControl c);
Validator v = ...;
Validate f0 = v.validate; // Safe.
AsyncValidate f1 = v.validate; // BAD assignment.
SyncValidate f2 = v.validate; // BAD assignment.
Map syncValidate(AbstractControl c) { ... }
Future<Map> asyncValidate(AbstractControl c) { ... }
v = syncValidate; // Good assignment.
v = asyncValidate; // Good assignment.
In practice, the concrete validate method of the validator v will probably be assignable to one of f1 or f2, but its static type doesn't say which one, so both are considered bad assignments.
You should only very rarely have a non-abstract method that is declared as returning FutureOr. In most cases, it's better to just always return a Future or a non-Future, and declare the method as such. Then you can always use the function as returning FutureOr if you need to, but use the more precise type in cases where you need it.

Nancy register dependency with type argument

Nancy auto-registration of dependencies is having trouble resolving a dependency with a type argument, so I'm trying to manually register it and cannot figure it out.
public abstract class BaseE { }
public abstract class BaseS<T> where T : BaseE { }
public class E : BaseE { }
public interface ISomething { }
public class S<T> : BaseS<T> where T : BaseE, ISomething { }
I want ISomething to be auto-resolved to class S, but that's not working, so I created a custom bootstrapper:
public class CustomBootstrapper : DefaultNancyBootstrapper
{
protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
{
base.ConfigureApplicationContainer(container);
// Works fine, but not sure if necessary
container.Register<E, BaseE>();
// Cannot get the syntax right, doesn't compile
// ??? container.Register<ISomething, S<E>>();
}
}
I can't seem to figure out the syntax. The line container.Register<ISomething, S<E>>(); gives me a compile error: The type S<E> cannot be used as type parameter 'RegisterImplementation' in the generic type or method 'TinyIoCContainer.Register<RegisterType, RegisterImplementation()'. There is no implicit reference conversion from 'S<E>' to 'ISomething'
Please help me figure out what the correct syntax / correct way to register a dependency with a type argument.
The error was due to incorrect syntax as pointed out by qujck.
public class S<T> : BaseS<T> where T : BaseE, ISomething { }
should be
public class S<T> : BaseS<T>, ISomething where T : BaseE { }
The first syntax says that type T extends BaseE AND implements ISomething, the second syntax says that type T only extends BaseE (and S implements ISomething). Dependency auto-injection is now working as expected.

Grails - No signature of method is applicable for argument types

I'm starting with Grails (using mainly the Eclipse plugin) and have been having trouble with Grails reading Java src files - whether it's a jar in the bin folder, or a Java file in the src/java folder.
I've created an example of how I call from my controller, a Java static method, and for some reason there is a problem with the argument type. Here's the controller:
def attempt = {
int counter = 20
int val1 = 1
int val2 = 1
def list = Replicate.create(counter, val1, val2)
render list.size()
}
And here is the Java file in the src/Java folder:
public class Replicate {
public static LinkedList<Pair<Integer, Integer>> create (int count, int val1, int val2){
LinkedList<Pair<Integer, Integer>> list = new LinkedList<Pair<Integer,Integer>>();
for (int i = 0; i < count; i++){
list.add(new Pair<Integer, Integer>(val1, val2));
}
return list;
}
}
When I try and load the page for the Controller's method, I get:
URI:
/clive-toolbox/cliveServices/attempt
Class:
groovy.lang.MissingMethodException
Message:
No signature of method: static tst.Replicate.create() is applicable for argument types: (java.lang.Integer, java.lang.Integer, java.lang.Integer) values: [20, 1, 1] Possible solutions: grep(), iterator()
The line that is highlighted as the error is this (in the controller):
def list = Replicate.create(counter, val1, val2)
And in other cases, working the same way, Grails manages to compile the project but just doesn't execute the method. I assume I'm doing something wrong. Any ideas here?
Your exception correctly states that the method Replicate.create() is not available to the controller.
Check the following,
1. You have imported the class right.
2. Package name is correct.
3. You have cleaned your application.
Try all the three mentioned above, it might help if the method is working on other places.
I received the same error when trying to call a service method from a controller.
What I did wrong was that I had not typed: "servicename" at the top of the method in the controller where I called the service from.

Getting value of a class variable through mirroring via getField

I am trying to understand how the Mirrors Api works. Specifically, how to obtain the value of a field from its Symbol, using getField.
For the getField method, it should work for any Symbol which is a getter, and it might be implicit. I therefore understood this that getField could be called directly on fields. In the following code sample, the getters for a and b should be implictly defined.
But the code throws, complainining that it cannot find any getter.
Breaking on exception: object of NoSuchMethodError, and breaking in 'dart:mirrors-patch_mirrors_impl.dart' on native "ClassMirror_invokeGetter";
abstract class CheckInitialized {
bool hasNull() {
var im = reflect(this);
var cm = im.type;
cm.declarations.values.where((dm) => dm is VariableMirror)
.forEach((vm) {
print(cm.getField(vm.simpleName));
});
// If field is null, return true
// If no fields are null, return false
}
}
class Test extends CheckInitialized {
int a;
String b;
}
void main() {
var a = new Test();
print(a.hasNull()); // true
}
It feels wrong to have to explicitly define a getter for this to work, but I can't see why this is not working. Of course, mirrors.dart is still very much changing, so I inlude that this is for v1.2.0.
You are trying to run getField on the class mirror. Since a and b are instance fields the getField fails. If you change a and b to static the getField invocations will work.
Alternatively you need to invoke getField on the instance-mirror (im).

Resources