Skip to content

Add DefaultVisit method to the visitor interface and class#13258

Merged
TravisEz13 merged 5 commits intoPowerShell:masterfrom
daxian-dbw:defaultvisit
Jul 31, 2020
Merged

Add DefaultVisit method to the visitor interface and class#13258
TravisEz13 merged 5 commits intoPowerShell: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

@vexx32 vexx32 left a comment

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

rjmholt commented Jul 23, 2020

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.

@daxian-dbw daxian-dbw assigned rjmholt and unassigned TravisEz13 Jul 24, 2020
{
/// <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 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
Copy link
Copy Markdown
Member Author

daxian-dbw commented Jul 25, 2020

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.

@rjmholt rjmholt requested a review from JamesWTruher July 28, 2020 15:08
@rjmholt 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 TravisEz13 merged commit 4464d1b into PowerShell:master Jul 31, 2020
@daxian-dbw daxian-dbw deleted the defaultvisit branch July 31, 2020 20:34
@daxian-dbw daxian-dbw added this to the 7.1.0-preview.6 milestone Aug 10, 2020
@ghost
Copy link
Copy Markdown

ghost commented Aug 17, 2020

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

Handy links:

@sdwheeler
Copy link
Copy Markdown
Collaborator

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

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