dart-lang / dart-lang/language

Static classes

Open
#3,075 5 comments 5 reactions 0 assignees View on GitHub
feature
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

Record is a great feature, it brings us close to minimalistic data classes.
It will be nice to have ability to create statically typed class records with with full potential of records.
In additional to inline classes (https://github.com/dart-lang/language/issues/2727) it can provide a lot of new instruments.

Preliminary sintaxis:
```dart
static class Point on ({int x, int y}) {}
final p = Point(x: 0, y: 0);
final (:x, :y) = u2;

```

It will bring all existing class functinnality + additional methods and static methods

Should **NOT ALLOW**:
- change deficend constructors
- override existing methods or getters
- like a final it shouldn't be extendable

Should **ALLOW**:
- should suppord records
- should support cast orerator (in case record signature allow it)
- should general class
- define new mettods, getter, setter and static methods.
- equality method should be overridden with instance type check.

Sample for records:
```dart
static class User on ({String? firstName, String? lastName}) {
static User? fromJson(Map json) {
return switch (json) {
{
'firstName': String? firstName,
'lastName': String? lastName,
} =>
(firstName: firstName, lastName: lastName),
_ => null,
};
}

Map toJson() {
final (:firstName, :lastName) = this;
return {'firstName': firstName, 'lastName': lastName};
}
}

static class Employee on ({String? firstName, String? lastName}) {}

final record = (firstName: 'A', lastName: 'V');
final user = record as User;
final u2 = User.fromJson('{"firstName": "A", "lastName": "V"}');
final (:firstName, :lastName) = u2;
final e1 = record as Employee;
final e2 = user as Employee;
// Feature request: make a copy and mutate with spread operator support
final e3 = (...e1, firstName: 'D'); // Auto cast to Employee
final e4 = (...record, firstName: 'D') as Employee;
final e5 = (...user, firstName: 'D') as Employee;

assert(user != record); // different type
assert(e1 == e2); // same type ans data after cast
assert(e2 != user); // different type
assert(e3 == e4);
```

Also it will allow to add static methods to abstract classes

```dart
// app_colors.dart
import 'package:flutter/material.dart' as material;

static class AppColors on material.Colors {
static const Color myBusinessColor = Color(0x012345678);
}
```

And add functionality to extarnal pachage classes

```dart
// package:openapi/src/models/user.dart
class User {
final String firstName;
final String lastName;
}
```

```dart
import 'package:openapi/openapi.dart' as openapi;
static class User on openapi.User {
Map toJson
() => '{"firstName": firstName, "lastName": value.lastName};

static User? fromJson(Map json) {
if (json case {'firstName': String firstName, 'lastName: String lastName}) {
return Category((firstName: firstName, lastName: lastName));
}
return null;
}
}

final user = User.fromJson('{"firstName": "A", "lastName": "V"}');
```

Contributor guide

Open the contributing guide

Research direction

No project files, tests, or implementation entry points are named. Start by reviewing the linked Dart language issue on inline classes and the proposed syntax and semantics here. Done would require a settled language design covering construction, inheritance, casts, equality, records, and extension of external classes.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.