Skip to content

Improvements for classes #6418

Open
Open

Description

@Stephanevg

Original discussion in #6015

I have the following few things that keep bugging me when I write classes and would would be great to seem them appear in a next version (or at least a few of them)

  • Interface creation (Tracking Support interface declaration using classes #2223)
  • Abstract classes
  • Possibility to Override properties getters and setters (Tracking Properties with accessor and mutator methods #2219)
  • Creation of static and non static nested classes
  • Implementing comment based help in Classes
  • Comment based help for Methods
  • Remove the need to reload classes (Currently, when developing, one needs to kill the console, and reload the class to get the latest version. IT would make sense to have the same functionality as in functions: Just F5, and the old version of classes get overwritten by the new version, and we can use it immediatley).

Activity

  1. added
    WG-Language
    on Mar 19, 2018
  2. SteveL-MSFT commented on Mar 19, 2018

    @SteveL-MSFT
    Member

    cc Rob Holt (@rjmholt) if he wants to take on some of these

    Stéphane (@Stephanevg) thanks for opening this issue, I updated your description into checkboxes so we can track progress as the different items won't all be addressed at once.

  3. BrucePay commented on Mar 19, 2018

    @BrucePay
    Collaborator

    Stéphane (@Stephanevg) What do you mean when you say "Creation of static and non static internal classes". (I can think of a couple of interpretations but i'd like to hear what you're thinking.)

  4. KevinMarquette commented on Mar 20, 2018

    @KevinMarquette
    Contributor

    Here are some nice to have features

    • The order of classes (with inheritance) should not matter when in a single file
      As a module author, I concat all my classes and functions into the psm1 at publishing time.
    • The ability to create classes in a namespace or auto namespace based on module name.
    • Use of default values or optional parameters in constructors
    • A way to export classes from a module in a discoverable way
    • Add option to mark class as hidden so using module does not expose or import them. (private classes)
    • A Get-Class -Module type of command like Get-Command
    • The ability to create a class based cmdlet in PowerShell

    I want to ask for true private classes and members, but I am aware of why that does not work in PowerShell.

  5. SteveL-MSFT commented on Mar 20, 2018

    @SteveL-MSFT
    Member

    Stéphane (@Stephanevg) Kevin Marquette (@KevinMarquette) it would be great if you can order your lists from highest priority to lowest priority

  6. Stephanevg commented on Mar 20, 2018

    @Stephanevg
    Author

    Bruce Payette (@BrucePay) The main need I have behind this one is to implement the Builder Pattern.

    In my module RegardsCitoyensPS, some of the constructors I have build are simply HUGE, implementing the Builder patter would allow me reduce this.

    I want to create a static internal class called Builder to whom I would delegate the mechanism of the creation of the Object. In the end, I will simplify the process of creation of an instance of my class for the engineers using my class.

    Example

    This is my use case:

    Current -->

    Class Depute{
        [int]$id
        [String]$Nom
        [String]$Prenom
        [Sexe]$Sexe
        [DateTime]$DateNaissance
        [String]$LieuNaissance
        [String]$Groupe
        [String]$NomCirconscription
        [int]$numcirco
        [int]$PlaceHemicylce
        [DateTime]$DebutDeMandat
        [String]$Profession
        [String]$Twitter
        [int]$NombreDeMandats
        [String]$partirattfinancier
        [Mandat[]]$autresmandats
        [String[]]$Collaborateurs
        [String[]]$Emails
     
    }  
    
        Depute([int]$id,[String]$Nom,[String]$Prenom,[String]$Groupe,[DateTime]$DateNaissance,[String]$LieuNaissance,[Sexe]$Sexe,[string]$nomcirco,[int]$numcirco,[int]$PlaceHemicylce,[DateTime]$DebutDeMandat,[String]$Profession,[string]$Twitter,[int]$NbMandats,[string]$partirattfinancier,[Mandat[]]$autresmandats,[string[]]$Collaborateurs,[string[]]$Emails){
            $this.id = $id
            $this.Nom = $Nom
            $This.Prenom = $Prenom
            $This.Groupe = $Groupe
            $this.DateNaissance = $DateNaissance
            $this.LieuNaissance = $LieuNaissance
            $This.Sexe = $Sexe
            $this.NomCirconscription = $nomcirco
            $this.numcirco = $numcirco
            $this.PlaceHemicylce = $PlaceHemicylce
            $this.Profession = $Profession
            $This.Twitter = $Twitter
            $this.DebutDeMandat = $DebutDeMandat
            $this.NombreDeMandats = $NbMandats
            $this.partirattfinancier = $partirattfinancier
            $this.autresmandats = $autresmandats
            $this.Collaborateurs = $Collaborateurs
            $this.Emails = $Emails
    
        }
    }

    Ho I imagined implementing the Builder Pattern to solve this:

    Class Depute{
        [int]$id
        [String]$Nom
        [String]$Prenom
        [Sexe]$Sexe
        [DateTime]$DateNaissance
        [String]$LieuNaissance
        [String]$Groupe
        [String]$NomCirconscription
        [int]$numcirco
        [int]$PlaceHemicylce
        [DateTime]$DebutDeMandat
        [String]$Profession
        [String]$Twitter
        [int]$NombreDeMandats
        [String]$partirattfinancier
        [Mandat[]]$autresmandats
        [String[]]$Collaborateurs
        [String[]]$Emails
     
      Hidden Depute([Builder]$Builder){
        $this.Id = $Builder.Id
        $this.Prenom = $Builder.Prenom
        $this.Nom = $Builder.Nom
         #etc...
    
      }
    
      Static Class Builder {
         [int]$id
         [String]$Nom
          [String]$Prenom
          [Sexe]$Sexe
          [DateTime]$DateNaissance
          [String]$LieuNaissance
          [String]$Groupe
          [String]$NomCirconscription
          [int]$numcirco
          [int]$PlaceHemicylce
          [DateTime]$DebutDeMandat
          [String]$Profession
          [String]$Twitter
          [int]$NombreDeMandats
          [String]$partirattfinancier
          [Mandat[]]$autresmandats
          [String[]]$Collaborateurs
          [String[]]$Emails
       
         [Builder] Id($Id){
          $This.id = $id
          return $this
         }
         [Builder] Nom($Nom){
           $this.Nom = $nom
           return $this
        }
       [Builder] Prenom($Prenom){
        $this.Prenom = $prenom 
        return $this
       }
    
       [Builder] Depute Groupe($Groupe){
          $This.Groupe = Groupe
           return $this
        }
    
       #etc... for every property.
    
       #The Build Method which actually would create the object
       [Depute] Depute Build(){
         
         Return [Depute]::New($This)
    
         }
    
    }

    Usage

    In the end, I hope to reduce the complexity for the consumers of this class, and make the usage of a long constructor more straightforward and easier. The example above would result in something like this:

     [Depute]::New().Builder().Prenom("Emanuel").Nom("Macron").Groupe("LREM").Build()

    That is how I imagined it.

    Today, I managed to make it work using a static method that would call [Depute]::New() and methods in [Depute] to add the different properties and that return $this.

    It works, but I actually liked the idea of having an internal class to attach functionality to it, and build my class through that Static class.

    Is that what you had in mind Bruce Payette (@BrucePay) ?

  7. Stephanevg commented on Mar 20, 2018

    @Stephanevg
    Author

    Steve Lee (@SteveL-MSFT) done. To me, everything that would help us to simply the implementation of Design patterns should be highest prio. (Interfaces and Abstract Classes are the two main ones for me).

    Adding help, is really secondary in my opinion, BUT really necessary.

  8. rjmholt commented on Mar 20, 2018

    @rjmholt
    Collaborator

    Stéphane (@Stephanevg) By internal here you mean a nested or inner class rather than an internal namepsace functionality? (I infer your primary language is French, just want to make sure I have the right idea)

  9. Stephanevg commented on Mar 20, 2018

    @Stephanevg
    Author

    Also, There is another small pain I have with Classes, is how we have to load them.
    As a lot of PowerShell users, for functions, I had a folder Private and Public, containing several .ps1 files. Each file, contained a function, which I dot sourced via the .psm1

    This way of organizing an item per file is not possible using classes, since a Class must be the very first thing to be loaded. Which means, the very first line of a Script.

    In the end, as Kevin Marquette (@KevinMarquette) mentionned, Either during Build all the Classes will be merged in one psm1 file, or they are all already in a single file during developement.

    What I understood from the powershell team members present at psconf in Germany, is that this behaviour was really difficult to change.

    I was hoping that that we could find a solution for this.

    Maybe, a new property in a .psd1 file with a property like ClassFolderToLoad = @(.\Classes\Public\) which would automatically load the classes during each module load could be an idea?

    But this is for me really a small small point. The main topics for me at the moment are Interface and Abstract classes.

  10. Stephanevg commented on Mar 20, 2018

    @Stephanevg
    Author

    Hi Rob Holt (@rjmholt)

    Oui, My first language is French ;)
    and oui, you are correct. I meant nested / inner class, when I referred to Internal class. (I updated the first post mentionning Nested Classes instead of internal)

  11. KevinMarquette commented on Mar 21, 2018

    @KevinMarquette
    Contributor

    I updated the order on my list.

  12. SamPosh commented on Mar 21, 2018

    @SamPosh

    If all the mentioned things are added then is that not called C#?

  13. KevinMarquette commented on Mar 21, 2018

    @KevinMarquette
    Contributor

    If we add all the mentioned thing then is that not called C#?

    We would like all the good class features that C# has, but allow us to do it in PowerShell. This is a valid request because calling out to PowerShell from C# is not near as easy as just writing PowerShell.

  14. markekraus commented on Mar 21, 2018

    @markekraus
    Contributor

    SamPosh (@SamPosh) Also, many of the features requested here are common polymorphic class features in many Object Oriented programming languages, not just C#. Classes in PowerShell open up a language-native Object Oriented programming paradigm. Unfortunately, the current implementation of classes in PowerShell offer little benefit over advanced features of PSCustomObjects which has hindered adoption. Addition of features requested in this issue improve Object Oriented paradigm and bring is closer to parity with other Object Oriented languages.

  15. 8 remaining items

  16. added
    Issue-Enhancement
    on Apr 14, 2018
  17. Stephanevg commented on Oct 18, 2018

    @Stephanevg
    Author

    Hi, I was wondering if any preogress has been done on this one? Steve Lee (@SteveL-MSFT) It seems like this one is not assigned anymore. It would be great to have these features / fixes implemented for 6.2

  18. SteveL-MSFT commented on Oct 19, 2018

    @SteveL-MSFT
    Member

    Stéphane (@Stephanevg) unfortunately at this time, we can't commit to addressing this issue in 6.2 timeframe perhaps 1 or 2 items may make it, but the team is busy with other necessary work right now

  19. Jaykul commented on Oct 1, 2019

    @Jaykul
    Contributor

    Or in the 7.x timeframe.

  20. fixinggunsinair commented on Apr 21, 2020

    @fixinggunsinair

    There should be a way to reference $this class static members in class methods and property default values.

    Example of how it works now (at least in Windows PowerShell):

    class Test {
        [String] static $Foo = "Value1"
        [String] static $Bar
        static Test() {
            [Test]::Bar = [Test]::Foo + "Value2"
        }
    }

    There's no $this::Foo thing.

    We should be able to use readonly modifier for class properties in some way, to make them readonly. But, we can't use fields in classes... We can use getter and setter approach for this, but there's no way to make setter private/protected. So, basically, there's no way to make member readonly or writeable for internal use. Every PSClass is exposed by default.

    There's no support for namespaces in PSClasses. There's no way to even basically mimic namespaces, like [ModuleName.ClassName]. PowerShell automatically assume what there's no way should exist two classes with same name, using scoping of classes instead.

    I could mention the Generics, but these ones are extreme for PowerShell for now. So at least PowerShell should support using them from assemblies, allow using generic methods (#5146) or allow generic inheritance (I could think of inheriting from IDictionary<String,Object>).

  21. dkaszews commented on Jul 18, 2023

    @dkaszews
    Contributor

    I think it would be awesome to get something like C# 9 records. I primarily use classes over [PSCustomObject] just to get reasonable ToString() and Equals(). This requires bunch of boilerplate, some of it I can abstract away with a helper returning a tuple of all properties to forward Equals() and GetHashCode().

  22. added
    Resolution-No Activity
    on Jan 15, 2024
  23. Shayan-To commented on Jan 15, 2024

    @Shayan-To

    Is there any progress, or any other issue tracking the last check box (Remove the need to reload classes)?

  24. removed
    Resolution-No Activity
    on Jan 15, 2024
  25. Stephanevg commented on Jan 15, 2024

    @Stephanevg
    Author

    I do not believe we should close this issue.

    This issue has not had any activity in 6 months, if there is no further activity in 7 days, the issue will be closed automatically.

    Activity in this case refers only to comments on the issue. If the issue is closed and you are the author, you can re-open the issue using the button below. Please add more information to be considered during retriage. If you are not the author but the issue is impacting you after it has been closed, please submit a new issue with updated details and a link to this issue and the original.

    I don't believe we should close this issue. It was mentioned a few times that this would receive some more love in the next Coming versions, but It looks like investments have been made in other areas that had a higher priority.

    Is there some buffer for this one now Steve Lee (@SteveL-MSFT) ? It has been open for a while, and it would be great to have at least some of the points crossed out one day.

  26. added
    KeepOpen
    WG-NeedsReview
    on Apr 29, 2024
  27. hifron commented on Nov 3, 2024

    @hifron

    Modules pwsh lang docs with Script modules: howto

    about Using docs

    pwsh GUI

    now arbitrary C# class(maybe huge) needs to be compiled into dll libs(assemblies :)(not only on Windows :) and be called and some boilerplate bee(or not so ugly be) made, so nice command CLI programs could be made and also GUI when required.

    but MSFT is not going the easy way: Make Nice C# program Easy to Setup without Much Infra Needs(think Python) and be easily called from pwsh scripts which could admins, users do some script publishing... or daily automation jobs... with maybe fixing some coverage issues... maybe on BI Lakes Excel fronts... or db access from markdown, maybe from MS Fabric R samples...

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-EnhancementKeepOpenWG-LanguageWG-NeedsReview

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions