gpt4 book ai didi

dart - 我该如何剥离顶级函数, getter 和 setter

转载 作者:行者123 更新时间:2023-12-03 03:07:22 28 4
gpt4 key购买 nike

如果我有一个类似以下的库,如何创建撕纸:

main.dart

int someFunc(int x, int y) => x * y;
int get val1 => 3;
int get val2 => 4;

main() {
// create tear-offs of someFunc, val1, and val2 here
}

最佳答案

当导入带有前缀的库时,这是可能的

other_lib.dart

int someFunc(int x, int y) => x * y;
int get val1 => 3;
int get val2 => 4;

class OtherClass {
int someFunc(int x, int y) => x * y;
int get val1 => 3;
int get val2 => 4;
}

main.dart
import 'other_lib.dart' as o;

void main() {
var _x = o#someFunc;
var _v1 = o#val1;
var _v2 = o#val2;

print('_v1: ${_v1}, ${_v1()}');
print('_v2: ${_v2}, ${_v2()}');
print('_x: ${_x}, ${_x(_v1(), _v2())}');
}

但它也可以在同一文件中

main.dart
import 'main.dart' as m;

int someFunc(int x, int y) => x * y;
int get val1 => 3;
int get val2 => 4;

void main() {
var _x = m#someFunc;
var _v1 = m#val1;
var _v2 = m#val2;

print('_v1: ${_v1}, ${_v1()}');
print('_v2: ${_v2}, ${_v2()}');
print('_x: ${_x}, ${_x(_v1(), _v2())}');
}

分析器尚无法识别语法,但是代码在VM中运行正常(出血边缘)。另请参阅 https://github.com/dart-lang/sdk/issues/24877

在两个示例中均打印

_v1: Closure: () => int from Function 'get:val1': static., 3
_v2: Closure: () => int from Function 'get:val2': static., 4
_x: Closure: (int, int) => int from Function 'someFunc': static., 12

关于dart - 我该如何剥离顶级函数, getter 和 setter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33646150/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com