Powershell: Assigning to an array literal of variables (LHS) should support Deconstruct methods

Created on 7 Aug 2018  路  12Comments  路  Source: PowerShell/PowerShell

Steps to reproduce

$tuple = [Tuple]::Create(1, 1)
$item1, $item2 = $tuple

Expected behavior

Tuple.Item1 is assigned to $item1 and Tuple.Item2 is assigned to $item2

Actual behavior

$tuple is assigned to $item1

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.1.0-preview.4
PSEdition                      Core
GitCommitId                    6.1.0-preview.4
OS                             Microsoft Windows 10.0.15063
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

While Tuple types aren't used frequently in PowerShell there are a few other default types that have Deconstruct methods like DictionaryEntry and KeyValuePair<,>.

Issue-Enhancement WG-Engine

Most helpful comment

For tuples, i definitely agree. (In fact I think we should have more support for tuples (i.e. being able to cast an array to a tuple) being able to "index" on a tuple so $mytuple[0] is equivalent to $mytuble.Item2, etc.).

All 12 comments

For tuples, i definitely agree. (In fact I think we should have more support for tuples (i.e. being able to cast an array to a tuple) being able to "index" on a tuple so $mytuple[0] is equivalent to $mytuble.Item2, etc.).

Would love to be able to do stuff like this.

$domain, $username = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name -split "\"

instead of this

$arr = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name -split "\"
$domain = $arr[0]
$username = $arr[1]

@thezim Your sample already works. (on Windows with -split "\")

It would be neat to have language syntax for tuples.

The title is misleading as this already works with arrays:
$one, $two = 1,2 works as expected.

It doesn't work with tuples, though, and I agree that should be rectified.

@vexx32 Sorry for the confusion in terminology, when I refer to an array literal assignment I mean an AssignmentStatementAst where the left hand side is an ArrayLiteralAst.

Please update the issue header.

Thanks @iSazonov, title has been updated.

Also I should fully clarify what I mean by Deconstruct.

C# 7 added support for assigning each item of a tuple to a different variable in a single assignment.

Tuple<int, string> tuple = Tuple.Create(1, "test");
var (first, second) = tuple;

The way this works is by looking for an instance or extension method named Deconstruct that has out parameters for each item. For tuple, this works because of the extension methods in System.TupleExtensions. The Deconstruct extension method that would be used in the above example has this signature:

public static void Deconstruct<int, string>(this Tuple<int, string> value, out int item1, out string item2);

More information

An extension method? Hmm. Well, as long as it's being called from the core parsing logic side of things that should be OK since you're still in a C# realm.

Extension methods aren't available from PS itself at present, but this should still work, I think

We have an issue about extension methods.

Yeah, I don't know...

I think any feature that needs extension method support should be stuck waiting for extension method support -- like the rest of us. I don't like the idea that we might support this one extension method just because we wish to call it automatically 馃槢

@Jaykul the feature request itself doesn't actually require extension method resolution, though it would make it a lot more consistent. That said, can you elaborate a bit more on the objection? If a system for resolving extension methods internally within the engine was added for this feature, it would partially add support with less risk. That'd make a full implementation potentially a lot easier down the road.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andschwa picture andschwa  路  64Comments

DarwinJS picture DarwinJS  路  65Comments

msftrncs picture msftrncs  路  62Comments

joeyaiello picture joeyaiello  路  66Comments

SteveL-MSFT picture SteveL-MSFT  路  66Comments