Chuyển đến nội dung chính

Bài đăng

Hiển thị các bài đăng có nhãn Refactoring

Dart 3.13 primary constructors: less boilerplate, one real breaking change

Dart 3.13 ships alongside Flutter 3.47, and its headline feature is the one Dart developers have been asking for since the language got null safety: primary constructors are stable . Declare your fields and your constructor in the class header, and delete the three-line ceremony that every model class in your codebase currently repeats. There is also a breaking change hiding in the release, and it is not the one most people expect. The basic shape The traditional form: class Point { int x; int y; Point ( this .x, this .y); } The primary constructor form: class Point ( var int x, var int y); That is the entire class. Note the ordering — the modifier comes first, then the type, then the name: var int x , not int var x . Declaring parameters are the actual feature This is the part worth understanding properly, because it is where the design is subtler than “shorter syntax”. A parameter prefixed with var or final is a declaring parameter : it induces an insta...

Primary constructor trong Dart 3.13: bớt boilerplate, một thay đổi phá vỡ thật sự

Dart 3.13 ra cùng Flutter 3.47, và tính năng tiêu điểm là thứ lập trình viên Dart đòi từ hồi ngôn ngữ có null safety: primary constructor đã stable . Khai báo field và constructor ngay trên header của class, và xoá đi ba dòng nghi thức mà mọi class model trong codebase của bạn đang lặp lại. Trong bản phát hành cũng có một thay đổi phá vỡ ẩn mình, và nó không phải thứ phần lớn mọi người đoán. Hình dạng cơ bản Dạng truyền thống: class Point { int x; int y; Point ( this .x, this .y); } Dạng primary constructor: class Point ( var int x, var int y); Đó là toàn bộ class. Chú ý thứ tự — modifier đứng trước, rồi tới kiểu, rồi tới tên: var int x , không phải int var x . Declaring parameter mới là tính năng thật Đây là phần đáng hiểu cho kỹ, vì thiết kế ở đây tinh tế hơn câu “cú pháp ngắn hơn”. Một tham số có tiền tố var hoặc final là declaring parameter : nó sinh ra một instance field. Tham số không có modifier nào hành xử như một đối số constructor thông thường v...