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

QOL Improvements for Classes #19969

Open
agowa opened this issue Jul 14, 2023 · 2 comments
Open

QOL Improvements for Classes #19969

agowa opened this issue Jul 14, 2023 · 2 comments
Labels
Issue-Enhancement the issue is more of a feature request than a bug Needs-Triage The issue is new and needs to be triaged by a work group. WG-Engine core PowerShell engine, interpreter, and runtime

Comments

@agowa
Copy link

agowa commented Jul 14, 2023

Summary of the new feature / enhancement

I'd like to suggest a few small quality of live improvements to PowerShell Classes.

  1. Allow chaining Constructors (instead of having to an additional hidden internal function)
class T {
  T() {$this.T("something")}
  T($something) {Do-Something}
}
  1. Fix default value handling for methods and functions in classes (this syntax is currently valid, but the default values are just ignored, requiring to create extra function definition to specify the default values in a way that actually works):
class T {
  something($mandatory, $optional="", $optional2=$false) {}
}
[T]::new().something("mandatoryvalue") | Should -Work # ParentContainsErrorRecordException: Cannot find an overload for "something" and the argument count: "1".
  1. Add support for parameter validation to avoid boilerplate validation code for simple things like ValidateSet or NotNullOrEmpty
  2. Add support for typed properties that aren't FixedSize. This could be archived by adding a (virtual) generic for ArrayList to use it like [System.Collections.Arraylist[String]] instead of [String[]] to get a "Non IsFixedSize" version of it. The main annoyance currently is that .Add() doesn't work when the list IsFixedSize.
  3. Add an Enum Type (or better a annotation or attribute) that serializes and deserializes as the value instead of it's index key to replace [ValidateSet('string1', 'string2','string3')][string]$variable with a proper type without requiring a bunch of custom serialization and deserialization code. So that ConvertTo-Json and ConvertFrom-Json continue to work, but that proper types can be used for the attribute instead of having to check for the string value within the script.
  4. Document that having a constructor with a single parameter but without type class T{ T() {}; T($name) {}} breaks type casting if not implemented explicitely within that constructor. Currently the documentation only mentions the parameterless one, but not the one with a single parameter without a specified type (is this intentional or a bug?)
class T{
  [string]$a
  [string]$b = "some default"
  [string]$name

  T() {}
  T($name) {
    $this.name = $name
  }
}
[T]@{
  a="Test"
  b="Values"
  name="SomeName"
} # Should return an object of Type T with a, b, and name initialized to the specified values, but instead it'll pass the HashTable to the T($name) constructor, which is currently not documented in the constructor section of about_Classes
  1. Not technically classes related, but as classes drop everything that is not explicitly returned, having a modifier for normal functions (outside of classes) to act the same would be great to get rid of $null = and | Out-Null in almost every line of such a function... [DefaultOut(Out-Null)] function Do-Something() { Do-Something -WithOutput; return "Only thing that is returned"}

Proposed technical implementation details (optional)

No response

@agowa agowa added Issue-Enhancement the issue is more of a feature request than a bug Needs-Triage The issue is new and needs to be triaged by a work group. labels Jul 14, 2023
@daxian-dbw daxian-dbw added the WG-Engine core PowerShell engine, interpreter, and runtime label Jul 21, 2023
@ThomasNieto
Copy link
Contributor

Related #6418

@mklement0
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Enhancement the issue is more of a feature request than a bug Needs-Triage The issue is new and needs to be triaged by a work group. WG-Engine core PowerShell engine, interpreter, and runtime
Projects
None yet
Development

No branches or pull requests

4 participants