This is a minor problem and the behavior is as designed, but perhaps the error message can be improved.
A dynamicparam {} block requires an advanced function's other code to be in begin {}, process{} and end {} blocks.
Instead trying to use dynamicparam in the function's main block alongside top-level code fails obscurely:
Steps to reproduce
Function foo {
[CmdletBinding()] param()
DynamicParam {
}
# Placing code here, outside of begin {}, process {} or end {} fails obscurely.
'hi'
}
Expected behavior
Actual behavior
At /Users/mklement/Desktop/pg/pg.ps1:33 char:14
+ Function foo {
+ ~
Missing closing '}' in statement block or type definition.
It is not obvious from this error message what the problem is.
Enclosing 'hi' in end { ... } fixes the problem.
Aside from a more helpful error message, perhaps allowing this code is an option (where the top-level code would run in an implied end {} block),
though perhaps that requires special-cased parsing behavior that is not worth the trouble.
Environment data
PowerShell Core v6.0.2 on macOS 10.13.3
PowerShell Core v6.0.2 on Ubuntu 16.04.3 LTS
PowerShell Core v6.0.2 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Windows PowerShell v5.1.15063.674 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
This is a minor problem and the behavior is as designed, but perhaps the error message can be improved.
A
dynamicparam {}block requires an advanced function's other code to be inbegin {},process{}andend {}blocks.Instead trying to use
dynamicparamin the function's main block alongside top-level code fails obscurely:Steps to reproduce
Expected behavior
Actual behavior
It is not obvious from this error message what the problem is.
Enclosing
'hi'inend { ... }fixes the problem.Aside from a more helpful error message, perhaps allowing this code is an option (where the top-level code would run in an implied
end {}block),though perhaps that requires special-cased parsing behavior that is not worth the trouble.
Environment data