Skip to content

Add DefaultVisit method to the visitor interface and class - #13258

Merged
Travis Plunk (TravisEz13) merged 5 commits into
PowerShell:masterfrom
daxian-dbw:defaultvisit
Jul 31, 2020
Merged

Travis Plunk (TravisEz13) merged 5 commits into
PowerShell:masterfrom
daxian-dbw:defaultvisit

Conversation

@daxian-dbw

Copy link
Copy Markdown
Member

PR Summary

Fix #12070

  1. Add the public virtual method DefaultVisit to AstVisitor, ICustomAstVisitor and DefaultCustomAstVisitor.
  2. Add default implementation => DefaultVisit(ast) to all existing methods in ICustomAstVisitor and ICustomAstVisitor2.
  3. Add change the current default implementation to methods in AstVisitor, AstVisitor2, DefaultCustomAstVisitor and DefaultCustomAstVisitor2 to be => DefaultVisit(ast).

PR Checklist

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

I have one question pertaining to the default implementation of DefaultVisit() -> is it better to return null there or to throw an exception?

@rjmholt

Copy link
Copy Markdown
Collaborator

is it better to return null there or to throw an exception?

I think if you target something like PSStd, you won't be able to override the default (you can't see the method since it's only in PS 7.1). Handling a null in that case is much simpler than an exception.

Comment thread test/xUnit/csharp/test_DefaultVisit.cs Resolved
Comment thread test/xUnit/csharp/test_DefaultVisit.cs Resolved
Comment thread test/xUnit/csharp/test_DefaultVisit.cs Resolved
Comment thread test/xUnit/csharp/test_DefaultVisit.cs Resolved
{
/// <summary/>
public virtual object VisitErrorStatement(ErrorStatementAst errorStatementAst) { return null; }
public virtual object DefaultVisit(Ast ast) => null;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this if we already have it in the interface?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default implementation of an interface method behaves like an explicit implementation, meaning that it's not visible to an instance of DefaultCustomAstVisitor, and you have to cast it to ICustomAstVisitor in order to call the method.
So, without this explicit implementation, you will have to write code like this:

public virtual object VisitErrorStatement(ErrorStatementAst errorStatementAst) => ((ICustomAstVisitor)this).DefaultVisit(errorStatementAst);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarify!

you will have to write code like this

I believe this is implied when working with interfaces.

Also I think now (in the PR context) we want to prefer interfaces.

It worries me that we are creating two entities that were supposed to be one.

@iSazonov

Copy link
Copy Markdown
Collaborator

I'd add Obsolete attribute because:

  • developers can suppress the warning
  • in any case they have to make many changes in code
  • it is better to get this before next LTS to get more feedback and we have more time for corrections.

@iSazonov Ilya (iSazonov) added CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log Documentation Needed in this repo Documentation is needed in this repo labels Jul 24, 2020
@daxian-dbw

Dongbo Wang (daxian-dbw) commented Jul 25, 2020 •

Copy link
Copy Markdown
Member Author

developers can suppress the warning

It's a breaking change with little or no value. We still need to keep this class around because it's part of PS 5.1 and 7.0, and thus we will have to keep updating it to avoid confusion to reading the code.

in any case they have to make many changes in code

Why would you assume this? It's very possible the user code has a visitor derives from DefaultCustomVisitor which doesn't need to change at all with new addition of AST types. SymbolResolvePostActionVisitor is a good example of it. I don't have to change it, but only for completeness. I reverted my changes to SymbolResolvePostActionVisitor.

Comment thread test/xUnit/csharp/test_DefaultVisit.cs Resolved
@rjmholt Rob Holt (rjmholt) added the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Jul 28, 2020
@ghost ghost removed the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Jul 30, 2020
@TravisEz13
Travis Plunk (TravisEz13) merged commit 4464d1b into PowerShell:master Jul 31, 2020
@daxian-dbw
Dongbo Wang (daxian-dbw) deleted the defaultvisit branch July 31, 2020 20:34
@ghost

Copy link
Copy Markdown

🎉v7.1.0-preview.6 has been released which incorporates this pull request.:tada:

Handy links:

@sdwheeler

Copy link
Copy Markdown
Contributor

Dongbo Wang (@daxian-dbw) Ilya (@iSazonov) If this needs documentation then it needs to be added to /// comments in the source code.

Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log Documentation Needed in this repo Documentation is needed in this repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ast visitor APIs missing "DefaultVisit"

7 participants