1: Generic.List of tuples
I can create a [Generic.List]
of Strings with [System.Collections.Generic.List[String]]::new()
, but I can't get a [Generic.List]
of Tuples with [System.Collections.Generic.List[System.Tuple]]::new()
, I have to use [System.Collections.Generic.List[Object]]::new()
. But what is weird is it doesn't fail here, it fails when I add. So this
$queue = [System.Collections.Generic.List[System.Tuple]]::new()
$tuple = [System.Tuple]::Create('One', 'Two', 'Three')
$queue.Add($tuple)
$queue
fails with Cannot find an overload for "Add" and the argument count: "1".
at line 3. But a list of objects works fine. I would rather not use [Object]
since that allows me to add anything to my list and I would rather be a bit more regorous with my typing. But more importantly I want to understand WHY [System.Collections.Generic.List[System.Tuple]]
doesn't work.
EDIT: As seen below, if I type the members of the tuple, and then cast the values in the tuple Create()
it works. But that still begs the question, why does an untyped tuple not work? I think this is basically academic, since I would prefer to type the values of the tuple as well as typing the members of the List. Rigor means complete rigor, not sloppy, half baked rigor.
2: Typed tuple
This blog shows the use of New-Object
to create a Tuple with typed members using $tuple = New-Object “tuple[String,DateTime,Int]” “Joel Bennett”, “July 1, 2014”, 6
. I wonder if there is a .NET constructor approach that I just can't find? This shows var tuple2 = new Tuple<string, double>("New York", 32.68);
but I have yet to figure out how to do that in PowerShell.
EDIT: So it seems the issue here is that you can't depend on PS automatic typing inside .Create()
, which makes sense. So, cast the values and it works.
$queue = [System.Collections.Generic.List[System.Tuple[String, DateTime, Int]]]::new()
$tuple = [System.Tuple]::Create([String]“Joel Bennett”, [DateTime]“July 1, 2014”, [Int]8)
$queue.Add($tuple)
$queue
3: Named Tuple members
This talks about the ability of VB to control the names of the Tuple members, since 'Item#' is both not very helpful and lame that these start at 1 while the Tuple is 0 indexed, which just annoys me. :) The question is, is this a VB only feature of Tuples? Or does a .NET tuple offer this functionality too, and in a way that is accessible from PS?