Skip to content

Having the ability to create structures in Dart #4629

@qardgroupadmin

Description

@qardgroupadmin

Greetings, I was recently reading the comments on a proposal I made, which discusses implicit constructors, and the idea came to me because the Dart language doesn't have the typical structures that use the reserved word struct and implicit constructors. What better way to explain something than by showing a code example to provide better context:

This is an example of what a basic structure would look like.

struct User {
    active: bool,
    username: String,
    email: String,
}

void main() {
  final myUser = User(true, "maria", "[email protected]");

  print(myUser.username); //print: maria
}

This is an example of how to instantiate a structure using dot shorthands.

struct User {
    active: bool,
    username: String,
    email: String,
}

void main() {
  final User myUser = .new(true, "maria", "[email protected]");

  print(myUser.username); //print: maria
}

The purpose of having structs in the Dart language is to give meaning to classes that only carry data, acting as a specific abstraction for that particular use case. You can read more about what structs are in the official Rust documentation.

Rust structures

The structures give meaning to the code and allow for simplified interoperability with low-level languages ​​such as Rust and C++.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureProposed language feature that solves one or more problems

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions