Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upAdd RFC: Native support for interface declarations #152
Conversation
felixfbecker
commented
Nov 21, 2018
|
The semantics section seems to talk a lot about the AST representation but not about the semantics and uses of interfaces. I think interfaces would be awesome in a language like PowerShell if they work like in TypeScript (duck typed, not like in C#). PowerShell works a lot with JSON from APIs or PSCustomObject literals. Ducktyped interfaces would allow to validate input objects without imperative type checks and declare output types of functions that return API JSON objects (for autocompletion). |
|
@felixfbecker Thanks for your feedback, I will expand on use cases in the RFC draft. With regards to JSON, it sounds more like we need better support for JSON deserialization and schema validation. While I agree, I fail to see how it relates to the contents of this RFC |
| # interface declaration with a method | ||
| interface IMyInterfaceWithMethod { | ||
| [void] DoSomething([int]$IntParam) |
This comment has been minimized.
This comment has been minimized.
BrucePay
Jan 28, 2019
Contributor
I presume the [void] is unnecessary as it is likewise unnecessary in classes. You might want to include such an example.
|
|
||
| ```powershell | ||
| # interface declaration with no members | ||
| interface IMyInterface { } |
This comment has been minimized.
This comment has been minimized.
BrucePay
Jan 28, 2019
Contributor
Note that this is a breaking change with a reasonably high likelihood of colliding with a command named "interface". Not saying we shouldn't do it, just that we should point this out in the proposal.
This comment has been minimized.
This comment has been minimized.
joeyaiello
Mar 3, 2020
Member
@JamesWTruher and I agree that breaking is undesirable, and we did find this one instance of a real-world module that would break, but we may still decide to do this via an experimental flag and message heavily around the breakage if we decide the value is high enough
| } | ||
| ``` | ||
|
|
||
| *Note: [C# 8 is slated to support default implementations](https://blogs.msdn.microsoft.com/dotnet/2018/11/12/building-c-8-0/) for interface methods, something we might want to consider as well* |
This comment has been minimized.
This comment has been minimized.
|
|
||
| Currently, when the parser recognizes a `class` or `enum` declaration, it produces a `TypeDefinitionAst` made up of `PropertyMemberAst` and `FunctionMemberAst` children. The type definition AST is then dispatched to either TypeDefiner.DefineTypeHelper or TypeDefiner.DefineEnumHelper for creating and emitting the corresponding runtime type. | ||
|
|
||
| We can either add new `AbstractMemberAst` types specifically for the purpose of abstract type definitions, or modify the existing `MemberAst` types to account for abstract member constraints. |
This comment has been minimized.
This comment has been minimized.
BrucePay
Jan 28, 2019
Contributor
Can you elaborate on this? And perhaps add examples to the previous section? There is a strong preference at the Committee level for lots of illustrative (practical) examples.
|
|
||
| Going with separate AST types for abstract type members would reduce the number of branches and special cases required in the rest of the code base, but the obvious benefit of reusing existing AST types for member declarations is that we can do not have to extend the existing AST visitor surface (ie. no `IAstVisitor3 : IAstVisitor2`, `AstVisitor3 : AstVisitor2, IAstVisitor3` etc.). This may present hidden parsing issues for tooling developers though. | ||
|
|
||
| Regardless of design changes to the AST surface, we can either define a new `TypeDefiner.DefineInterfaceHelper` for emitting interface types, or we can modify the existing `DefineTypeHelper` definition to take the `Interface` attribute into account and modify all the other emitting functions being called by it. |
This comment has been minimized.
This comment has been minimized.
BrucePay
Jan 28, 2019
Contributor
I would think that there would be enough common code that it makes sense to generalize the DefineTypeHelper. Another thing to consider is if it makes sense to handle abstract classes in the process.
| There are a couple of open questions around the design and implementation of interface declaration support: | ||
|
|
||
| - Should we allow control over getter/setter declarations? | ||
| - If so, should we replicate the syntax from C#? |
This comment has been minimized.
This comment has been minimized.
BrucePay
Jan 28, 2019
Contributor
I would say yes. As far as C# syntax goes, there's the traditional stuff but there are also expression-valued getters (which are cool.) to consider. However I suspect that conservative might be the best approach.
| - Should we allow control over getter/setter declarations? | ||
| - If so, should we replicate the syntax from C#? | ||
| - Existing class definitions need to [mark interface member implementations virtual](https://github.com/PowerShell/PowerShell/issues/8302) | ||
| - Depending on community feedback and envisioned use cases, are interfaces sufficient or might we consider `abstract` classes as well? |
This comment has been minimized.
This comment has been minimized.
| ## Motivation | ||
|
|
||
| As a module author, | ||
| I can leverage common OO design patterns that require abstract type definitions, |
This comment has been minimized.
This comment has been minimized.
BrucePay
Jan 28, 2019
Contributor
Also how about "I can interoperate with .NET frameworks that require implementing interfaces'
|
|
||
| # Native Support for .NET Interface Declarations | ||
|
|
||
| The PowerShell Classes feature introduced in PowerShell 5.0 allows for type definitions at runtime. Since open sourcing PowerShell Core, one of the most oft-repeated feature request I've heard from community members is the ability to also declare and define interfaces at runtime, usually for ease of implementing common software design pattern such as the Builder pattern. |
This comment has been minimized.
This comment has been minimized.
BrucePay
Jan 28, 2019
Contributor
Could you include a small illustrative example of what you want to achieve?
|
@daxian-dbw @rjmholt are going to take a look at the prototype implementation here. @PowerShell/powershell-committee agrees we should merge this as experimental sooner rather than later to understand how folks like this might break. We realized in playing with the existing reserved keywords (
To that end, we can potentially mitigate the impact of the breakage by introducing a runtime warning or PSSA rule that gets thrown when a user defines a function called interface, and recommending that they use the call operator when invoking that function. |
IISResetMe commentedNov 21, 2018
RFC for new interface declaration feature, working prototype available as of IISResetMe/PowerShell@f98fb2b