Skip to content

Support interface declaration using classes #2223

Open

Description

@vors

Copied from TFS task 150409

We should be able to write something along these lines

interface IFoo
{
  [string] foo();
}

class Foo : IFoo
{
  [string] foo() { return 'foo' }
}

Activity

  1. added
    WG-Language
    on Sep 10, 2016
  2. GeeLaw commented on Sep 11, 2016

    @GeeLaw

    Also, explicit implementation.

  3. forensicsguy20012004 commented on Oct 5, 2016

    @forensicsguy20012004

    I agree, this would be a great addition to powershell

  4. added
    Up-for-Grabs
    on Nov 2, 2016
  5. changed the title [-]Interface declaration[/-] [+]Interface declaration in classes[/+] on Nov 2, 2016
  6. changed the title [-]Interface declaration in classes[/-] [+]Support interface declaration using classes[/+] on Nov 2, 2016
  7. added
    Hacktoberfest
    on Feb 20, 2018
  8. IISResetMe commented on Apr 10, 2018

    @IISResetMe
    Collaborator

    I'd love to work on an implementation of this (I already have a ref implementation in PowerShell: https://github.com/IISResetMe/Legislator).

    The creation and emission of new types seem pretty straightforward, but I'd need some help with the parsing bits, adding new keywords and expression trees

  9. SteveL-MSFT commented on Apr 13, 2018

    @SteveL-MSFT
    Member

    Dongbo Wang (@daxian-dbw) can help you out, but he's going to be attending PSConf.EU next week

    cc Rob Holt (@rjmholt)

  10. rjmholt commented on Apr 13, 2018

    @rjmholt
    Collaborator

    Mathias R. Jessen (@IISResetMe) I saw your lightning demo and went to your repo afterward. Really amazing work. Your interface DSL actually inspired me, both in how rich it was and since it means interfaces could be made definable in PowerShell without necessarily adding them as a universal language feature. With that said, more complicated things like inheritance might not be so straightforward and would probably need to be part of the parser.

    There's a discussion of some of the class-specific stuff in #6418 as well.

    But for settling on the syntax and general design, the best place to start might be with an RFC, just to drive discussion on how interfaces should look and what features should be supported in PowerShell. Especially so that the PowerShell Committee can consider it.

  11. IISResetMe commented on Apr 13, 2018

    @IISResetMe
    Collaborator

    Steve Lee (@SteveL-MSFT) Cool, I'll be at PSConfEU as well, will try to catch up with Dongbo Wang (@daxian-dbw)!

    Rob Holt (@rjmholt) Thank you for the kind words. I'll try to add support for inheritance to Legislator over the next week, but based on the MSIL generated by CSC for multiple inherited interfaces it looks quite trivial to add.

    If we go for something like what sergei (@vors) suggest here, interfaces (including inheritance) would require just a subset of the existing grammar already in place for classes, the only thing missing is basically the interface keyword itself 😃

    I'll try to write up an RFC in the coming weeks and submit it to the RFC repo!

  12. IISResetMe commented on Nov 13, 2018

    @IISResetMe
    Collaborator

    Thanks to a few pointers from Dongbo Wang (@daxian-dbw) last month I got unstuck again 🎉

    If anyone wants to check out a working implementation of parser support for interface declarations, I've pushed it here: https://github.com/IISResetMe/PowerShell/tree/feature/interfaces

    I still need to add a type compilation step for interfaces in TypeOps, already have a basic routine working just need to merge it into that branch, will try and add it during this week

  13. IISResetMe commented on Nov 16, 2018

    @IISResetMe
    Collaborator

    Here we go: IISResetMe@156e010

    Parser rules for interface members are still slightly buggy (; not recognized as statement terminator atm), but it at least produces correct interface types as can bee seen below 😄

    image

    Syntax in this implementation also doesn't support get/set control, but it's arguably of limited value unless we decide on whether class properties in PowerShell should behave like fields or actualy properties (at which point we need to generate getters and setters implicitly)

  14. 5 remaining items

  15. joeyaiello commented on Mar 10, 2020

    @joeyaiello
    Contributor

    To be clear, Dongbo Wang (@daxian-dbw) and Rob Holt (@rjmholt) can review the implementation, but they don't currently have the time to take it and run it towards a production implementation. Are you still interested in taking it all the way to the finish line if that's the case?

  16. vexx32 commented on Mar 10, 2020

    @vexx32
    Collaborator

    If necessary, I can pick up the remainder of the necessary work once y'all are done reviewing what's currently done.

    I will probably need you to brief me on what's still left in the to-do pile, though, Mathias R. Jessen (@IISResetMe) 😁

  17. Xpyder commented on Jan 21, 2021

    @Xpyder

    I think this is the right place for this but not 100% sure so if it's not please point me in the right place to post

    I'm trying to figure out how to implement Composition Over Inheritance in my powershell scripts.

    I think it would go like this:

    Diagram for clarity:

    2021-01-20_17-32-32

    # Actions #
    interface Swims {
        [bool] Swim();
    }
    
    interface Lands {
        [bool] Land( $land_spot );
    }
    
    interface Flies : Lands {
        [bool] Fly( $destination );
    }
    
    
    # Animals #
    class Duck : Flies, Swims {
        Duck(){}
        
        [bool] Fly( $destination, $land_spot ){
            $this.Fly($destination);
            $this.Land($land_spot)
            return $true
        }
    
        [bool] Fly( $destination ){
            write-host "The duck flies to $($destination)"
            return $true
        }
    
        [bool] Land( $land_spot ){
            write-host "The duck lands at $($land_spot)"
            return $true
        }
    
        [bool] Swim(){
            write-host "The duck paddles it's flippers idly"
            return $true
        }
    }
    
    class Dove : Flies {
        [bool] Fly( $destination, $land_spot ){
            $this.Fly($destination);
            $this.Land($land_spot)
            return $true
        }
    
        [bool] Fly( $destination ){
            write-host "The dove flies to $($destination)"
            return $true
        }
    
        [bool] Land( $land_spot ){
            write-host "The dove lands at $($land_spot)"
            return $true
        }
    }
    
    
    # Declarations #
    $duck = [Duck]::New()
    $dove = [Dove]::New()
    
    function Fly-To([Flies]$flier, $destination){ return $flier.Fly($destination) }
    function Swim-Idle([Swims]$swimmer){ return $swimmer.Swim() }
    
    
    # Tests #
    
    ## Should Work ##
    Fly-To $duck "Minnesota"
    Fly-To $dove "New York"
    Swim-Idle $duck
    
    
    ## Should Fail ##
    
    # should error because $dove doesn't implement [Swims]
    # or if Duck Typing then because it doesn't implement $_.Swim()
    Swim-Idle $dove
    

    The net result being that I don't have to create different versions of Fly-To for Duck and Dove, but Swim-To gives a meaningful error when trying to make the Dove swim

    I take it these aren't currently implemented (I can't find documentation for this anywhere) so I was going to submit a feature request but I think this ticket is a match for this pattern? But let me know if it should be a separate issue

  18. vexx32 commented on Jan 21, 2021

    @vexx32
    Collaborator

    Yeah, this more or less would allow for that pattern in full.

    From discussions with Mathias R. Jessen (@IISResetMe) as far as I recall, he had the features more or less ready to go, but there were some design decisions he felt shouldn't be made alone and he was trying to solicit feedback on his implementation as Joey Aiello (@joeyaiello) mentioned above.

    I don't think that's gone anywhere thus far, though, as far as I can recall the implementation hasn't really had a good look that I'm aware of.

  19. Strada-EricFr commented on Jan 3, 2023

    @Strada-EricFr

    No update on the capability to support : interface ?

  20. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    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.

  21. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    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.

  22. added
    Resolution-No Activity
    on Nov 16, 2023
  23. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    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.

  24. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    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.

  25. added
    KeepOpen
    and removed
    Resolution-No Activity
    on Nov 5, 2025
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

    KeepOpenUp-for-GrabsWG-Language

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions