Skip to content

Champion: allow 'default' in deconstruction #1394

Not planned
@jcouv

Description

@jcouv

Allow (int i, string s) = default; and (i, s) = default.

  • Proposal added
    Discussed in LDM (by email so far)
    Decision in LDM
    Finalized (done, rejected, inactive)
    Spec'ed

Initially raised in #1358 (thanks @KnorxThieus)
Prototype in decon-default branch

Activity

self-assigned this
on Mar 17, 2018
added this to the 8.0 candidate milestone on Mar 19, 2018
AustinBryan

AustinBryan commented on Jun 11, 2018

@AustinBryan

This will be super nice to do in constructors:

class MyClass
{
    int foo;
    string bar;
    byte baz

    public MyClass() => (foo, bar, baz) = default;
}
jnm2

jnm2 commented on Jun 11, 2018

@jnm2
Contributor

@AustinBryan Those fields are already zero-inited, so what is the purpose of that constructor?

modified the milestones: 8.0 candidate, 8.X candidate on Jul 18, 2019
modified the milestones: 8.X candidate, Any Time on Sep 11, 2019
alrz

alrz commented on Sep 12, 2019

@alrz
Member

The use case I had in mind for this is assigning a bunch of out parameters on a failure case,

bool TryGet(out int i, out double d) {
  // ...
  (i, d) = default;
  return false;
}

btw I think this is already implemented by jcouv, why the Any Time milestone?

jcouv

jcouv commented on Sep 12, 2019

@jcouv
MemberAuthor

The implementation I have is not quite complete because we've recently implemented more target-typing scenarios (switch expressions in particular). So (i, d) = expr switch { true => (1, null); _ => throw null; }; should work too (ie. giving the tuple in branch a type).

added
Needs ImplementationThe specification for this issue has been approved, it needs an implementation
on Oct 15, 2020
Rekkonnect

Rekkonnect commented on Jul 8, 2022

@Rekkonnect
Contributor

Taking some time into the code regarding this case, and a side-effect would have to also be matching the individual types of the tuple expression:

(1, default)
(default, "")
(1, null)

should match as (int, string), whereas

(1, default)
(null, "")

should match as (int?, string).

I'm awaiting for a response on whether it's okay to follow this approach in another branch targeting the features/decon-default branch, which could be considered a relevant but different feature.

Rekkonnect

Rekkonnect commented on Jul 24, 2023

@Rekkonnect
Contributor

@jcouv are there any updates on this moving forward? #1358 is still in the Working Set, and the only scenario that I believe was indeed left is the target typing from switch expressions.

locked and limited conversation to collaborators on Nov 19, 2024
removed their assignment
on Nov 19, 2024
jcouv

jcouv commented on Nov 19, 2024

@jcouv
MemberAuthor

Closing as duplicate of #1358

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs ImplementationThe specification for this issue has been approved, it needs an implementationProposal champion

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @333fred@alrz@gafter@MadsTorgersen@jnm2

        Issue actions

          Champion: allow 'default' in deconstruction · Issue #1394 · dotnet/csharplang