Repository navigation
Description
This idea arose from this from the stackoverflow question Convert datetime formats using powershell and is related to #7445 We need a new operator '..' to access to members of childitems of a collection but specific to the ToString method.
Variable definitions (used in this request)
$Strings = 'Monday, November 30, 2020 7:55:01 AM', 'Sunday, October 18, 2020 9:10:01 PM', 'Sunday, November 8, 2020 9:40:34 PM'
$DateTimes = [DateTime[]]$StringsKnowing that ToString isn't just parameterless but also has an usefull IFormatProvider interface parameter, I wanted to prove that you can use member enumerating for the stackoverflow question (instead of iterating through each item), but I failed to do so, as I keep ending up with a (new) collection that has it's own (parameterless) ToString method.
As Michael Klement (@mklement0) commented in #7445:
ToStringis currently invariably invoked on the collection itself (where it is more or less useless)
-
If it is indeed useless, why not break it?
That probably goes too deep as it is not just PowerShell but theToStringmethod actually appears to be implemented in all the .Net collections. -
If it can't be broken, why not bend it? Meaning, only apply the method to the collection if it has no arguments, which would let the developer choose between the collection (
$DateTimes.ToString()) or a the embedded scalars ($DateTimes.ToString('')) , knowing that currently this curectly returns an error.
But that is probably too inconsistent and confusing...
Hmm, talking about inconsistency...
$DateTimes[0].ToLocalTime()
Monday, November 30, 2020 8:55:01 AM
$DateTimes[0..2].ToLocalTime()
Monday, November 30, 2020 8:55:01 AM
Sunday, October 18, 2020 11:10:01 PM
Sunday, November 8, 2020 10:40:34 PM
$DateTimes[0].ToString('yyyy-MM-dd HH:mm:ss')
2020-10-18 21:10:01
$DateTimes[0..2].ToString('yyyy-MM-dd HH:mm:ss')
MethodException: Cannot find an overload for "ToString" and the argument count: "1".- If it can't be broken or bended, why not equipping all scalar objects (that have a
ToStringmethod and explicitly excluding collections) with anFormatalias for theToStringmethod, so that it is possible to enumerate string formats like (wishful thinking):
$DateTimes.Format('yyyy-MM-dd HH:mm:ss')
2020-11-30 07:55:01
2020-10-18 21:10:01
2020-11-08 21:40:34
Activity
mklement0 commentedon Dec 10, 2020
My vote is to solve this problem with the proposed
%.operator for unambiguous member enumeration - which would allow you to call$someCollection%.ToString('...')- for the reasons summarized in #7445 (comment).SeeminglyScience commentedon Dec 10, 2020
As a workaround you can use the engine intrinsic method
ForEach:iRon7 commentedon Nov 16, 2023
I guess this propose doesn't have enough support to keep alive and therefore should be labeled as Resolution-Declined
Anyways, I decided to closed it.