Skip to content

Add Format alias for the ToString method to all scalar objects #14372

Closed

Description

@iRon7

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[]]$Strings

Knowing 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:

ToString is 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 the ToString method 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 ToString method and explicitly excluding collections) with an Format alias for the ToString method, 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

  1. added
    Issue-Enhancement
    on Dec 10, 2020
  2. mklement0 commented on Dec 10, 2020

    @mklement0
    Contributor

    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).

  3. SeeminglyScience commented on Dec 10, 2020

    @SeeminglyScience
    Contributor

    As a workaround you can use the engine intrinsic method ForEach:

    $DateTimes.ForEach('ToString', 'yyyy-MM-dd HH:mm:ss')
  4. added
    WG-Language
    on Dec 12, 2020
  5. added
    Resolution-No Activity
    on Nov 16, 2023
  6. iRon7 commented on Nov 16, 2023

    @iRon7
    Author

    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.

  7. removed
    Resolution-No Activity
    on Nov 16, 2023
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

    Issue-EnhancementWG-Language

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions