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

Add RFC: Native support for interface declarations #152

Open
wants to merge 3 commits into
base: master
from

Conversation

@IISResetMe
Copy link

IISResetMe commented Nov 21, 2018

RFC for new interface declaration feature, working prototype available as of IISResetMe/PowerShell@f98fb2b

@felixfbecker
Copy link

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.
There is also no motivation or use case explanation.

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).

@IISResetMe
Copy link
Author

IISResetMe commented Nov 21, 2018

@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

IISResetMe added 2 commits Nov 21, 2018
# interface declaration with a method
interface IMyInterfaceWithMethod {
[void] DoSomething([int]$IntParam)

This comment has been minimized.

@BrucePay

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.

@BrucePay

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.

@joeyaiello

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.

@BrucePay

BrucePay Jan 28, 2019 Contributor

I had exactly the same thought!


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.

@BrucePay

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.

@BrucePay

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.

@BrucePay

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.

@BrucePay

BrucePay Jan 28, 2019 Contributor

😁

## Motivation

As a module author,
I can leverage common OO design patterns that require abstract type definitions,

This comment has been minimized.

@BrucePay

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.

@BrucePay

BrucePay Jan 28, 2019 Contributor

Could you include a small illustrative example of what you want to achieve?

@daxian-dbw daxian-dbw self-requested a review Mar 3, 2020
@joeyaiello
Copy link
Member

joeyaiello commented Mar 3, 2020

@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 (from, var, and define) that token reservation doesn't actually block the definition of the function, and you can still call the function name by using the call operator (&):

C:\Users\Joey> function var {"var"}
C:\Users\Joey> var
ParserError:
Line |
   1 |  var
     |  ~~~
     | The 'var' keyword is not supported in this version of the language.

C:\Users\Joey> & var
var

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

4 participants
You can’t perform that action at this time.