Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Classes: an uninitialized [string] property defaults to $null rather than the empty string #7294

Closed
mklement0 opened this issue Jul 16, 2018 · 7 comments
Labels
Resolution-No Activity Issue has had no activity for 6 months or more WG-Engine core PowerShell engine, interpreter, and runtime

Comments

@mklement0
Copy link
Contributor

This is an inconsistency discovered by @alx9r:

If you define a class property of type [string] without assigning an initial value, its value is unexpectedly $null, even though in all other contexts PowerShell defaults [string] values to '' (the empty string) and even converts explicitly assigned $null to ''.

Steps to reproduce

class c { [string] $prop }; $null -eq [c]::new().prop
class c { [string] $prop = $null }; $null -eq [c]::new().prop
class c { [string] meth() { return $null } }; $null -eq [c]::new().meth()

Expected behavior

False
False
False

Actual behavior

True
False
False

That is, the uninitialized [string] property unexpectedly contains $null.

Environment data

PowerShell Core v6.1.0-preview.3 on macOS 10.13.5
PowerShell Core v6.1.0-preview.3 on Ubuntu 16.04.4 LTS
PowerShell Core v6.1.0-preview.3 on Microsoft Windows 10 Pro (64-bit; Version 1709, OS Build: 16299.371)
Windows PowerShell v5.1.17134.48 on Microsoft Windows 10 Pro (64-bit; Version 1709, OS Build: 16299.371)
@iSazonov iSazonov added the WG-Engine core PowerShell engine, interpreter, and runtime label Jul 16, 2018
@mattpwhite
Copy link

Is this unexpected? I know that I would personally be very surprised to find string members initialized to empty strings. I would expect class members to have default values as they do in other .NET languages. Coercing null to empty string is already a confusing foot gun in other contexts, I wouldn’t want to see it extended to classes.

@mklement0
Copy link
Contributor Author

mklement0 commented Jul 17, 2018

@mattpwhite:

The primary purpose of this issue is to show the lack of consistency.

I suggested resolving the inconsistency toward PowerShell-like behavior, because, as demonstrated, in the majority of cases you already do get the surprising behavior in the context of custom classes - in line with non-class PowerShell behavior:

  • assigning $null to a [string]-typed property or variable
  • returning $null from a [string]-typed method.

(Ideally, PowerShell itself should also default [string] instances to $null, but that ship has sailed)

That said, in other respects classes are closer to C# behavior, not least syntactically.

So I suppose resolving the inconsistency toward C#-like behavior (allowing assigning $null to [string] variables/properties / allowing returning $null from [string]-typed methods) is another option.

Both resolutions are technically breaking changes, so a third option is to simply live with the inconsistency and merely document it.

I can't really speak to the guiding principles behind the behavior of PowerShell classes.
Perhaps @lzybkr can help out.

@mattpwhite
Copy link

Point taken, and I agree that it’s a bit inconsistent. I guess I’m just voicing a preference for aligning with C# in this context. As you note, in many ways, it already is.

No one has ever had to learn about [NullString]::Value and been happy about it. I know it’s too late for that, but I’d hate to have it show up more than it already has to.

Copy link
Contributor

This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

2 similar comments
Copy link
Contributor

This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

Copy link
Contributor

This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

@microsoft-github-policy-service microsoft-github-policy-service bot added Resolution-No Activity Issue has had no activity for 6 months or more labels Nov 16, 2023
Copy link
Contributor

This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution-No Activity Issue has had no activity for 6 months or more WG-Engine core PowerShell engine, interpreter, and runtime
Projects
None yet
Development

No branches or pull requests

3 participants