Coder Social home page Coder Social logo

Partial Record destructuring about language HOT 4 OPEN

rrousselGit avatar rrousselGit commented on July 17, 2024 8
Partial Record destructuring

from language.

Comments (4)

munificent avatar munificent commented on July 17, 2024 2

Yeah, I agree this would be useful. As @tatumizer suggests, I do think it should be something you explicitly opt into, probably using the same ... rest syntax we have for list patterns.

However, as @lrhn notes, it's challenging to incorporate this into refutable patterns. If we support:

var stuff = (a: 1, b: 2, c: 3);
var (:a, :c, ...) = stuff;

Then users would probably expect this to work:

var stuff = (a: 1, b: 2, c: 3);
if (stuff case (a: 1, :var c, ...)) {
  print('a is one and c is $c');
}

But that might in turn lead them to expect this to work:

Object stuff = (a: 1, b: 2, c: 3); // <- Note: Has type Object now.
if (stuff case (a: 1, :var c, ...)) {
  print('a is one and c is $c');
}

Even though this looks similar, it's a very different program. Because now, what is the type that stuff is being tested against? Up to this point, every pattern is basically "test if the value has some type X and then do other stuff". But there's no type for "any record type that has these fields".

You can't just say "a record with fields a and c" because there's no subtype relationship between that record type and a record type like ({a: int, b: int, c: int}). In the literature, this is called "width subtyping" and we deliberately avoided it in Dart because it makes optimizations and other stuff harder.

We could say that you can use incomplete record patterns (i.e. ones with a ...) only in contexts where there is a surrounding context type that is a record type that fills in the .... That would work. I just worry that it would be a confusing limitation when users tried to use ... in a place where there isn't that context type.

In short, it's an interesting idea and would be practically useful. I just worry about the risk of user confusion.

from language.

water-mizuu avatar water-mizuu commented on July 17, 2024 1

You can also use the defined type alias to object destructure the record. However, I do support this feature.

void main() {
  final Big big = (field1: 42, field2: 42, field8: 42);
  final Big(:field1, :field2) = big;
}

from language.

tatumizer avatar tatumizer commented on July 17, 2024 1

A straightforward solution would be to enable the syntax like

final (:field1, :field2, ...rest) = big; 
final [x, y, ...rest] = [1, 2, 3, 4];

If the "rest" is of no interest, then

final (:field1, :field2, ..._) = big;
final [x, y, ..._] = [1, 2, 3, 4];

from language.

lrhn avatar lrhn commented on July 17, 2024

It's not completely unreasonable for a pattern with a record type as matched value type, to be able to ignore some of the fields.

The hurdle is that it's not allowed for a refutable match against a non-record type, because a record pattern must ensure that it is matched against a single known record shape.
That means case (: var x, ...) is only allowed against some matched value types, as the only refutable pattern that can be invalid because of the type being matched.

from language.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.