print address of object - dart

Is there any trick to print the address of a dart object?
I'm having an issue in which I have a standard enum class as described in Does Dart support enumerations?.
class Access {
static const IA = const Access._(0);
static const RO = const Access._(1);
static const RW = const Access._(2);
final int value;
const Access._(this.value);
...
}
The variable access is of type Access and the value is 1, yet access==Access.RO is returning false.
var access = _.access;
print('''
access => ($access, ${access.runtimeType}, ${access.value})
static => (${Access.RO}, ${Access.RO.runtimeType}, ${Access.RO.value})
equal ? => ${(access == Access.RO)}
''');
prints
access => (RO, Access, 1)
static => (RO, Access, 1)
equal ? => false
If I provide an operator==(Access other) that compares the values it returns the expected value. So, I figured maybe this has to do with coming at the class from different libraries (maybe isolate related) and if I could print the address of access and Access.RO I could see if they are different. Of course, if they were different I'd then need to know why as well.

When you deal with const you have to be very careful. If you use new Access._(0) instead of const Access._(0) you will not get the same object. Here is an example :
class Access {
static const IA = const Access._(0);
static const RO = const Access._(1);
static const RW = const Access._(2);
final int value;
const Access._(this.value);
}
main(){
print(Access.IA == const Access._(0)); // prints true
print(Access.IA == new Access._(0)); // prints false
}
This could explain your problem.

If identical(a, b) returns false you can be sure that their pointers are different. (The inverse is not necessarily true for numbers, but that's an implementation detail).
If you are dealing with isolates you need to be careful when you transmit objects. Even, if they are canonicalized on one side, they won't be, after they have been transmitted to another isolate:
import 'dart:async';
import 'dart:isolate';
class A { const A(); }
foo() {
port.receive((x, _) {
print(x == const A()); // Prints false.
});
}
main() {
var p = new ReceivePort(); // To keep the program running.
spawnFunction(foo).send(const A(), null);
}
Also note that dart2js doesn't allow to transmit arbitrary objects.

Related

Dart type that is implemented by everything

I'm not quite sure how to phrase this but I basically want to write a function like this:
import 'dart:async';
int countBroadcastStreams(List<Stream<Object>> streams) {
return streams.where((s) => s.isBroadcast).length;
}
void main() {
final a = StreamController<int>.broadcast();
final b = StreamController<String>.broadcast();
countBroadcastStreams([
a.stream,
b.stream,
]);
}
This works, however if I change one of the StreamControllers to StreamController<Null> then it doesn't because Null is the only type that doesn't inherit Object.
So my question is: which type should I use instead of Object so it works with Null too? dynamic seems to work, but is that the best option? Does it introduce additional runtime cost?
I don't think the problem comes from the Object type. Try to explicitly specify that the Stream objects can be null:
import 'dart:async';
int countBroadcastStreams(List<Stream<Object?>> streams) {
return streams.where((s) => s != null && s.isBroadcast).length;
}
void main() {
final a = StreamController<int>.broadcast();
final b = StreamController<Null>.broadcast();
print(countBroadcastStreams([
a.stream,
b.stream,
]));
}
// console log: 2
Specify the nullable type for the stream objects: List<Stream<Object**?**>> streams.

How to extend from const constructor

class and const gives a nice possibility to define structured constants. Now I try to extend from one, works ok. Try to use a constant from the inherited class, it works, still I only manage to get through with final instead of const. No big deal in my project, but is it possible to have it const?
class A {
final String name;
final int value;
const A(final String this.name, final this.value);
static const A ONE = const A('One', 1);
}
class B extends A {
const B(final String name, final int val) : super(name, val);
static const B B_ONE = const B('One', 1);//Ok
static const A A_ONE = A.ONE;//Ok
static final B BA_ONE = new B(A.ONE.name, A.ONE.value);//Ok
static const B BA_ONE_CONST = const B(A.ONE);//No B constructor!?!
}
Your B class indeed does not have a constructor that takes a single A instance. You haven't declared one, and you can't do it directly without a little workaround.
The problem is that B extends A, it doesn't just contain an A instance. So, if you want to start with an A instance (like A.ONE) and create a B instance from it, where B extends A and has its own name and value fields, you'll have to extract the name and value from the A to create the new object ... and you can't do property extraction in a const expression. So, that's a no-go.
What you can do is to have a different implementation of B that gets its values directly from an A instance, and then create a constant from that:
class B extends A {
const B(String name, int value) : super(name, value);
const factory B.fromA(A instance) = _SecretB;
...
static const B BA_ONE_CONST = const B.fromA(A.ONE);
}
class _SecretB implements B {
final A _instance;
const _SecretB(this._instance);
String get name => _instance.name;
int get value => _instance.value;
}
If you are the only one using the B.fromA constructor, you could just remove it and call the _SecretB constructor directly. If you want to expose it to other clients of your class, you can make it a const factory constructor like here.

should returning const value from const object be const?

I'm using the style class below to mimick enums (from Does Dart support enumerations?)
It is working fine in that this snippet produces expected results.
void main() {
InterpolationType it = InterpolationType.LINEAR;
print("it is $it and stringified ${stringify(it)}");
print(InterpolationType.fromJson(it.toJson()));
}
But the DartEditor is complaining about "Expected constant expression" in the case statements of fromJson method. Is there a const I can throw in somewhere to get rid of this complaint?
class InterpolationType {
static const LINEAR = const InterpolationType._(0);
static const STEP = const InterpolationType._(1);
static const CUBIC = const InterpolationType._(2);
static get values => [
LINEAR,
STEP,
CUBIC
];
final int value;
const InterpolationType._(this.value);
String toString() {
switch(this) {
case LINEAR: return "LINEAR";
case STEP: return "STEP";
case CUBIC: return "CUBIC";
}
}
int toJson() {
return this.value;
}
static InterpolationType fromJson(int v) {
switch(v) {
case LINEAR.value: return LINEAR;
case STEP.value: return STEP;
case CUBIC.value: return CUBIC;
}
}
static InterpolationType fromString(String s) {
switch(s) {
case "LINEAR": return LINEAR;
case "STEP": return STEP;
case "CUBIC": return CUBIC;
}
}
}
As you discovered: accessing fields from a const object is not a constant operation. So the editor (as well as the VM and dart2js) are right.
With the current syntax there is no way to express a (informal) contract that a field of a class will always be a final field. For example, I could change the value-field to be a getter instead of a field. The interface-contract of the class definitely allows me to do that, because I never told anybody that I would keep "value" as a field. However if I did that it would break every program that relied on the existence of this final field.
As a consequence the current behavior is very unlikely to change.
However: in theory it would be possible to improve the Dart language so that you could use "const" instead of "final" for local fields, and initialize them with initializer lists. And in this case accessing the field could be considered a constant operation. I currently don't see any downsides to this behavior and it would be backwards-compatible.
// WARNING: What follows DOES NOT WORK, just a potential example
class InterpolationType {
const value; // Note the "const" instead of "final".
const InterpolationType._(this.value);
}
The language is already pretty stable but you can open a bug at http://dartbug.com/ and suggest this behavior. It's not very likely that the feature-request would be accepted, but it's definitely worth a try.

Unit test hanging when using lexical scoping and generics with extends

The behavior seems to be related to the presence of 'extends' as shown with unit test below:
typedef dynamic GetFromThing<T extends Thing>(T target);
typedef GetFromThing<T> DefGetFromThing<T extends Thing>(dynamic def);
typedef dynamic GetFromT<T>(T target);
typedef GetFromT<T> DefGetFromT<T>(dynamic def);
class Thing {
int value;
}
class Test {
static final GetFromThing<Thing> fromThingSimple = (Thing target) {
return target.value;
};
static final DefGetFromThing<Thing> fromThing = (dynamic def) {
return (target) => null;
};
static final DefGetFromT<int> fromInt = (dynamic def) {
return (target) => null;
};
}
main() {
test('this works', () {
var temp1 = Test.fromThingSimple(new Thing());
});
test('this works too', () {
var temp = Test.fromInt(10);
});
test('should let me call lexically closed functions', () {
var temp = Test.fromThing(10); // <-- causes test to hang
});
}
The fact that the VM hangs is clearly a bug. The code is legal. The fact that typedefs describe function types and can be generic whereas function types themselves are never generic is not an issue in principle (though it might be for the implementation).
I find it very interesting that type parameters in typedefs work without some kind of warning or error, since Dart doesn't have generic methods.
You very well may have come across two bugs here, the first that there's no errors, and the second that the VM hangs.

How can I build an enum with Dart? [duplicate]

This question already has answers here:
Does Dart support enumerations?
(8 answers)
Closed 7 years ago.
The Dart language does not have enums (yet??). What is the proper or idiomatic way to construct an enum, at least until a language feature arrives?
Dart now has support for enums.
The rest of this answer is for Dart <= 1.8. If using > 1.8, use Dart's formal support for enums (explained in another answer).
It's true, the Dart language does not (yet?) have enums. There is an open issue for it.
In the meantime, here is an idiomatic Dart snippet to create your own enum.
class Enum {
final _value;
const Enum._internal(this._value);
toString() => 'Enum.$_value';
static const FOO = const Enum._internal('FOO');
static const BAR = const Enum._internal('BAR');
static const BAZ = const Enum._internal('BAZ');
}
Using const constructors means you can use this enum in a switch. Here's an example:
class Fruits {
final _value;
const Fruits._internal(this._value);
toString() => 'Enum.$_value';
static const APPLE = const Fruits._internal('APPLE');
static const PEAR = const Fruits._internal('PEAR');
static const BANANA = const Fruits._internal('BANANA');
}
void main() {
var yummy = Fruits.BANANA;
switch (yummy) {
case Fruits.APPLE:
print('an apple a day');
break;
case Fruits.PEAR:
print('genus Pyrus in the family Rosaceae');
break;
case Fruits.BANANA:
print('open from the bottom, it is easier');
break;
}
}
With r41815 Dart got native Enum support see http://dartbug.com/21416 and can be used like
enum Status {
none,
running,
stopped,
paused
}
void main() {
print(Status.values);
Status.values.forEach((v) => print('value: $v, index: ${v.index}'));
print('running: ${Status.running}, ${Status.running.index}');
print('running index: ${Status.values[1]}');
}
[Status.none, Status.running, Status.stopped, Status.paused]
value: Status.none, index: 0
value: Status.running, index: 1
value: Status.stopped, index: 2
value: Status.paused, index: 3
running: Status.running, 1
running index: Status.running
A limitation is that it is not possibly to set custom values for an enum item, they are automatically numbered.
More details at in this draft https://www.dartlang.org/docs/spec/EnumsTC52draft.pdf
I use a little bit simpler version of the Enum class in Dart Web Toolkit:
/**
* Emulation of Java Enum class.
*
* Example:
*
* class Meter<int> extends Enum<int> {
*
* const Meter(int val) : super (val);
*
* static const Meter HIGH = const Meter(100);
* static const Meter MIDDLE = const Meter(50);
* static const Meter LOW = const Meter(10);
* }
*
* and usage:
*
* assert (Meter.HIGH, 100);
* assert (Meter.HIGH is Meter);
*/
abstract class Enum<T> {
final T _value;
const Enum(this._value);
T get value => _value;
}
I like top-level constants for my enums. You can use imports to fix any collisions. This makes using the enums much less verbose.
i.e.
if (m == high) {}
instead of:
if (m == Meter.high) {}
Enum definition:
class Meter<int> extends Enum<int> {
const Meter(int val) : super (val);
}
const Meter high = const Meter(100);
const Meter middle = const Meter(50);
const Meter low = const Meter(10);

Resources