Declaring and initializing array of class objects in Dafny - dafny

I am trying to create a simple class that stores an array of users, another class as demonstrated here:
include "User.dfy"
class EnrollmentStation {
var x : array<User>;
constructor() {
x := new User[2];
}
}
I am getting this error on var x's declaration:
Undeclared top-level type or type parameter: User (did you forget to qualify a name or declare a module import 'opened'?)
I cannot find anything in the documentation that details declaring and initializing object arrays. Any suggestions?

Related

Dart gives Unhandled Exception: type is not a subtype of type of 'value'

I have an abstract class ImageUpdate. Two classes, NewImage and ExistingImage implement ImageUpdate.
I have a variable imageUpdates of type List<ImageUpdate>.
When I try adding a variable of type NewImage to the List, I get this error:
Unhandled Exception: type 'NewImage' is not a subtype of type 'ExistingImage' of 'value'
I am pretty stumped, since the list is of ImageUpdate and not ExistingImage, so I have no idea how to debug it, especially since Dart is type safe (nowhere am I using dynamic).
I suspect that you have code similar to:
class Base {}
class Derived1 extends Base {}
class Derived2 extends Base {}
List<Base> makeList() {
var list = [Derived1()];
return list;
}
void main() {
var list = makeList();
list.add(Derived2()); // TypeError
}
What happens is that the List object is originally created as a List<Derived1>. (That is, list.runtimeType will be something like List<Derived1>, not its static (declared) type of List<Base>.) Then, when you try to add a Derived2 object to list, it will fail at runtime because list's actual, runtime type is List<Derived1>, which does not allow Derived2 elements.
This ultimately stems from Dart allowing implicit type conversions from GenericClass<Derived> to GenericClass<Base> if Derived derives from Base. This is useful in many cases, but it also can lead to situations like this one that fail at runtime.
You can fix this by explicitly stating that you want a List<Base> object:
List<Base> makeList() {
var list = <Base>[Derived1()]; // Note the explicit type.
return list;
}
or, if that's not possible, by creating a new List<Base> object:
var list = <Base>[...makeList()];
var list = List<Base>.from(makeList());
(In this particular situation, List.of(makeList()) also would work since it would create a new List object with the static type, but I wouldn't use that since being explicit about the type conversion would be more readable.)

Adding the generic type to a comparable type in Dart

This is a followup question after reading this Q&A:
Generic Sorting function accepts T, but want to ensure T is comparable
I have a class like so:
class BinarySearchTree<E extends Comparable> { ... }
so I can create an instance like this:
final tree = BinarySearchTree<int>();
My question is about using Comparable vs Comparable<E>. When I do this:
class BinarySearchTree<E extends Comparable> { ... }
then the type defaults to E extends Comparable<dynamic>. I normally try to avoid dynamic, so in order to be more explicit about the type that is being compared, it seems like I should write it this:
class BinarySearchTree<E extends Comparable<E>> { ... }
But in that case I get an error here:
final tree = BinarySearchTree<int>();
// 'int' doesn't conform to the bound 'Comparable<int>' of the type parameter 'E'.
// Try using a type that is or is a subclass of 'Comparable<int>'.
This demonstrates my lack of understanding of generics. What am I missing?
In Dart, a class cannot implement 2 different concrete instances of a generic interface:
abstract class Foo<T> {}
// error: Foo can only be implemented once
class Bar implements Foo<String>, Foo<int> {}
num implements Comparable<num>, because it would be slightly absurd for the built-in number types to not be comparable. However, since int is a subtype of num (and therefore inherits Comparable<num>, it cannot have Comparable<int>.
This leads to the slightly weird consequence that int does not implement Comparable<int>.
The problem you're facing is that from the language's point of view, there are 2 types involved: the type of the elements being compared, and the type of the elements they are being compared to.
As such, your type will need 2 type parameters:
class Tree<T extends Comparable<S>, S> {
T get foo;
}
final intTree = Tree<int, num>();
final foo = intTree.foo; // returns an int
Admittedly, this isn't a super clean solution, but if you're using Dart 2.13 or higher, you can use typedefs to make it a bit nicer:
typedef IntTree = Tree<int, num>;
typedef RegularTree<T> = Tree<T, T>;
final intTree = IntTree();
final stringTree = RegularTree<String>();
intTree.foo // is an int
stringTree.foo // is a String
There is another option, which is to just drop some type safety and use Comparable<dynamic>, but personally I'd recommend against it. BTW, if you want to avoid accidentally missing type parameters you can disable implicit-dynamic as described here: https://dart.dev/guides/language/analysis-options#enabling-additional-type-checks
This will give an error any time the type dynamic is inferred from context without the programmer actually typing the word dynamic

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.

Errors when creating Realm object classes

I'm trying to write a simple app in Swift, backed with Realm. When I write vars within classes, I keep getting errors such as "Property cannot be implicitly #objc because its type cannot be represented in Objective-C". Sometimes these errors (which seem to involve Objective-C, which I'm not using) appear and sometimes they don't--See screenshots below.
I'm an admitted Newbie to Swift, but I thought I understood enough to get started. Specifically:
1) Should I add "#objc" before the class declaration?
2) Should I add "#objc" before the dynamic var declaration?
3) It seems that a Float var must be initialized, but not so for String?
4) Why does the compiler complain about a declaration on one line, but not about identical syntax in the next line?
Would sure appreciate some guidance, or pointers to specific instructions for creating Realm object classes!
I reckon the error comes from calling #objc instead of #objcMembers.
Try change
#objc class Transaction: Object
to
#objcMembers class Transaction: Object
And remove all #objc in front of your dynamic variables. So your final code should look like:
import Foundation
import RealmSwift
#objcMembers class Transaction: Object {
dynamic var picPath: String?
dynamic var transAmount: Float = 0.0
}
#objcMembers class Transaction: Object {
dynamic var currentBalance: Float = 0.0
dynamic var highWaterBalance: Float = 0.0
dynamic var acctName: String?
}
Our fellow Paul Hudson has described this attribute on his site hackingwithswift.

AS3: Extending The Dictionary Class - Accessing Stored Data

So I want to extend the dictionary class. Everything works so far except that in some of my methods that need to reference the dictionary's content I make a call like:
this[ key ]
It doesn't like that. It just tells me that there's no property 'key'. Is there a way to way to access the data within this class?
Also, I'm using an integer for the key.
Edit: I've found that the same behavior happens when you extend Array.
var myArray : Array = new Array();
trace( myArray[ 0 ] );
var myArrayExtender : ArrayExtender = new ArrayExtender();
trace( myArrayExtender[ 0 ] );
Where in this case, myArray returns "undefined" and myArrayExtender throws error 1069. ArrayExtender is an empty class that extends Array and calls super() in the constructor.
from what you say, i am quite sure you did not declare ArrayExtender as dynamic
in ECMAscript array access and property access are semantically equivalent ... #1069 happens, if you access an undefined property, on a sealed class, because thes do not allow adding properties at runtime ...
same thing for the Dictionary ...
greetz
back2dos

Resources