-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[vm/ffi] Struct.addressOf Pointer<Struct>.load() signatures #37284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Honestly, I feel that extension methods on all pointer types would be the best, e.g. // Say, NativeInt is a superclass to FFI's Int8, Int16, etc.
extension IntPointerExt on Pointer<T extends NativeInt> {
int load();
void store(int value);
}
extension DoublePointerExt on Pointer<T extends NativeDouble> {
// ...
} This removes the need for the type parameter on load, which seems redundant right now (I already have a Pointer, why do I need to specify that I'm loading an int?). The only unfortunate bit is that extension methods are... NYI. But I think it would be a great change to do in the future (although breaking). |
Yes, that should work fine with extension methods. It would look like this though: extension MyExtension<S extends Struct> on S {
Pointer<S> addressOf() {
// ...
}
} This would apply to any object with a type which extends |
@ds84182 that is exactly what we are aiming for! Unfortunately we have to wait until extension methods get implemented in Dart. Until then we'll have to use |
Plan is for extension methods to be ready in Q3, so not too far out. |
We could use this API today to have a clean API until extension methods, with a non-breaking migration path: class Pointer<T extends NativeType> {
// ...
dynamic get val; // FE ensure T !extends Struct
void set val(dynamic val); // FE ensure T !extends Struct
T get ref; // FE only allows when T extends Struct
// ...
} @leafpetersen : Will we get extension getters and setters? |
We should change |
Some documentation for the reasoning behind the above suggestion. Separation ref and val
Use of dynamicUsing dynamic for |
Yes. |
I think we've agreed on the design here -- the implementation remains in #37361. |
What should the API be to get from a
Struct
to aPointer
and vice versa?Struct to Pointer: Struct.addressOf
Field of struct
Pros:
.
notationCon:
Struct
Top level function
Pro:
Struct
Con:
.
notationMethod
Pro:
Struct
.
notationCon:
Extension method
@lrhn @leafpetersen is this supported by our design for extension methods?
Pro:
Struct
.
notationThis has the least amount of cons, if this would be supported.
Pointer to Struct: Pointer.load
Similar argument for getting from a
Pointer
to aStruct
by using load.Extension method
Pro:
Method
This is the current API.
Con:
Pre-extension methods
If the extension methods will be supported in the future, what should we decide for now - with the easiest path of migrating to extension methods later?
The text was updated successfully, but these errors were encountered: