ETS ToString() script methods aren't honored in expandable strings and casts
See original GitHub issueThe only type where a custom .ToString()
is consistently honored appears to be [pscustomobject]
.
With all others - irrespective of whether the script method is added at the type level (Update-TypeData
) or the instance level (Add-Member
) - only explicit .ToString()
calls honor the override; string interpolation and casts do not.
Steps to reproduce
# Override the .ToString() method for [hashtable]
# to output a compressed JSON representation.
Update-TypeData -Force -TypeName System.Collections.Hashtable -MemberName ToString -MemberType ScriptMethod -Value { $this | ConvertTo-Json -Compress }
$ht = @{ one = 1 }
$htAsJson = $ht | ConvertTo-Json -Compress
# Explicit .ToString() call: OK
$ht.ToString() | Should -Be $htAsJson
# !! BROKEN: [string] cast
[string] $ht | Should -Be $htAsJson
# !! BROKEN: string interpolation
"$ht" | Should -Be $htAsJson
Expected behavior
All tests should pass.
Actual behavior
Tests 2 and 3 fail, because the .ToString()
override wasn’t honored.
Strings differ at index 0. Expected: '{"one":1}' But was: 'System.Collections.Hashtable'
Environment data
PowerShell Core 7.2.0-preview.2
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:10 (6 by maintainers)
Top Results From Across the Web
Avoid toString() Fatal Error [String Casting in JavaScript]
The solution here is to use the String() casting function rather than using the most commonly used toString() function. Let's see how it...
Read more >How to change Get-Date commandlet default result value ...
ToString() method is unexpectedly not honored - see GitHub issue #14561; sadly, it appears that this bug won't get fixed. # Override .ToString()...
Read more >fixes from Rome Patch 4 Hot Fix 1 to Tokyo ...
From the scope that was created, run the script, 'new TestScript(). ... They are not honoring the gateway configuration and still look in...
Read more >Groovy in Action.pdf
applying the “Groovy truth” (see chapter 6) for casts to boolean, calling toString() for casts to string, and so on. The exhaustive list...
Read more >JamaicaVM 8.0 User Manual
method, either use the erase option of rpm or the provided uninstall script. Jamaica.remove. If super user privileges are not available, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Engine WG discussed this issue today. Our opinion is that the requested change is not likely to be trivial, as these different methods of casting happen at different points in the engine.
We agree it would make sense for these three casting methods to be consistently calling the ETS ToString() method, but it would be a breaking change to the existing behaviour at this point.
We would need to see a compelling use case where this is necessary to consider changing the existing behaviour and risk breaking existing scripts that may unintentionally be relying on the behaviour.
We have an issue with a request to support extension methods. Many find that offer very desirable. But if we want extension methods we will fall in the issue as result.