question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ETS ToString() script methods aren't honored in expandable strings and casts

See original GitHub issue

The 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:open
  • Created 3 years ago
  • Reactions:3
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
vexx32commented, Feb 18, 2022

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.

0reactions
iSazonovcommented, Feb 18, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found