飞镖中有结构吗?

Bre*_*k89 7 dart

在javascript中它总是困扰我的人们使用对象作为矢量{x: 1, y: 2}而不是使用数组[1,2].数组的访问时间比对象快得多,但是通过索引访问会更加混乱,尤其是在需要大型数组的情况下.我知道dart有固定数组,但有没有办法命名数组的偏移量,就像你在另一种语言中使用struct或tuple/record一样?可能定义枚举/常量?

我想要类似的东西

List<int> myVector = new List([x,y]);
myVector.x = 5;
Run Code Online (Sandbox Code Playgroud)

是否有相同或惯用的方法来做到这一点?

lrn*_*lrn 15

这听起来像是一堂课.

 class MyVector {
   final x;
   final y;
   const MyVector(this.x, this.y);
 }
Run Code Online (Sandbox Code Playgroud)

在运行时创建命名索引结构没有更简单,更有效的方法.为此,你通常使用a Map,但它不如真正的类有效.

类应该与固定长度列表一样有效(时间和内存).


Vin*_*ink 6

对我来说,我看到了两种方法.在我看来,我会以最好的方式排序

基于类的方法

这里,方法是将您的需求封装在专用对象中

优点:

  • 它是封装的
  • 您可以根据需要提出几种访问变量的方法
  • 您可以扩展功能而不会破坏所有内容
  • 我喜欢它:p

缺点

  • 花更多的时间来创建课程等.
  • 你真的需要我在职业选手中说的话吗?
  • 对于js人来说可能很奇怪

例如:

class Vector {
  int x;
  int y;

  static final String X = "x";
  static final String Y = "y";

  Vector({this.x, this.y});
  Vector.fromList(List<int> listOfCoor) {
    this.x = listOfCoor[0];
    this.y = listOfCoor[1];
  }

  // Here i use String, but you can use [int] an redefine static final member
  int operator[](String coor) {
    if (coor == "x") {
      return this.x;
    } else if (coor == "y") {
      return this.y;
    } else {
      // Need to be change by a more adapt exception :)
      throw new Exception("Wrong coor");
    }
  }
}

void main() {
  Vector v = new Vector(x: 5, y: 42);
  Vector v2 = new Vector.fromList([12, 24]);

  print(v.x); // print 5
  print(v["y"]); // print 42
  print(v2.x); // print 12
  print(v2[Vector.Y]); // print 24
}
Run Code Online (Sandbox Code Playgroud)

基于枚举的方法:

您还可以定义一个"枚举"(实际上并非真正实现,但将在未来版本中),它将包含您的值的"快捷方式"

优点

  • 实施起来更简单
  • 更像是你的榜样; p

缺点

  • 不太可扩展
  • 我觉得不是很漂亮
  • 不是OOP想的

例:

class Vector {
  static final int x = 0;
  static final int y = 1;
}

void main() {
  List<int> myVector = new List(2);
  myVector[Vector.x] = 5;
  myVector[Vector.y] = 42;
}
Run Code Online (Sandbox Code Playgroud)

做出选择; p


Gün*_*uer 5

这只能通过 Dart 中的类实现。

http://dartbug.com 上有一些开放的功能请求


JAr*_*Are 5

如果您具有相当大的数据结构,则可以将其"dart:typed_data"用作模型并为存储的数据提供轻量级视图。这样,开销应该最小。例如,如果您需要Uint8值的4X4矩阵:

import "dart:typed_data";
import "dart:collection";
import "package:range/range.dart";
class Model4X4Uint8 {
  final Uint8List _data;
  static const int objectLength = 4 * 4;
  final Queue<int> _freeSlotIndexes;
  Model4X4Uint8(int length): _data = new Uint8List((length) * objectLength),
        _freeSlotIndexes = new Queue<int>.from(range(0, length));
  int get slotsLeft => _freeSlotIndexes.length;
  num operator [](int index) => _data[index];
  operator []=(int index, int val) => _data[index] = val;
  int reserveSlot() =>
      slotsLeft > 0 ? _freeSlotIndexes.removeFirst() : throw ("full");
  void delete(int index) => _freeSlotIndexes.addFirst(index);
}
class Matrix4X4Uint8 {
  final int offset;
  final Model4X4Uint8 model;
  const Matrix4X4Uint8(this.model, this.offset);
  num operator [](int index) => model[offset + index];
  operator []=(int index, int val) => model[offset + index] = val;
  void delete() => model.delete(offset);
}
void main() {
  final Model4X4Uint8 data = new Model4X4Uint8(100);
  final Matrix4X4Uint8 mat = new Matrix4X4Uint8(data, data.reserveSlot())
      ..[14] = 10
      ..[12] = 256; //overlow;
  print("${mat[0]} ${mat[4]} ${mat[8]} ${mat[12]} \n"
        "${mat[1]} ${mat[5]} ${mat[9]} ${mat[13]} \n"
        "${mat[2]} ${mat[6]} ${mat[10]} ${mat[14]} \n"
        "${mat[3]} ${mat[7]} ${mat[11]} ${mat[15]} \n");
  mat.delete();
}
Run Code Online (Sandbox Code Playgroud)

但是,这是一个非常低级的解决方案,可以通过内存管理和溢出轻松创建偷偷摸摸的错误。