Skip to content

For better DSL #6194

Closed
Closed

Description

@p0W3RH311

this code work

function a 
{
   b {
        write-host 'hello'
   }
}

function b 
{
   param([scriptblock]$sb)
   &$sb
}

but this don't work because the character {

function a 
{
   b 
  {
        write-host 'hello'
   }
}

function b 
{
   param([scriptblock]$sb)
   &$sb
}
PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      6.0.0-rc.2
PSEdition                      Core
GitCommitId                    v6.0.0-rc.2
OS                             Microsoft Windows 6.3.9600
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Activity

  1. added
    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
    WG-Languageparser, language semantics
    on Feb 20, 2018
  2. markekraus commented on Feb 20, 2018

    @markekraus
    Contributor

    I can't see how this could ever be added.

    b
    {
        write-host 'hello'
    }

    Would currently returns the result of b and scriptblock. That may be desirable and changing the behavior would break the old behavior. This will probably not be doable with functions, but perhaps a new language keyword to distinguish DSL elements.

    something like

    function a 
    {
       b 
      {
            write-host 'hello'
       }
    }
    
    DSLfunction b 
    {
       param([scriptblock]$sb)
       &$sb
    }

    This is the reason I highly recommend OTBS or Stroustrup indentation in PowerShell. There are far fewer places you can use Allman and friends.

  3. lzybkr commented on Feb 20, 2018

    @lzybkr
    Contributor

    Dynamic keywords enable this scenario (first introduced with DSC).

    This #3169 was an attempt at properly supporting DSLs in PowerShell.

  4. markekraus commented on Feb 20, 2018

    @markekraus
    Contributor

    Jason Shirk (@lzybkr) awesome!
    p0W3RH311

    here is how you could make that work now:

    $keyword = [System.Management.Automation.Language.DynamicKeyword]@{
        Keyword ="b"
        BodyMode = [Management.Automation.Language.DynamicKeywordBodyMode]::ScriptBlock
        NameMode = [Management.Automation.Language.DynamicKeywordNameMode]::NoName
    }
    [System.Management.Automation.Language.DynamicKeyword]::AddKeyword($keyword)
    
    function a 
    {
       b 
      {
            write-host 'hello'
       }
    }
    
    function b 
    {
        param (
            [Management.Automation.Language.DynamicKeyword] $KeywordData,
            $Name,
            [ScriptBlock] $Value,
            $SourceMetaData,
            $InstanceName
        )
       & $Value
    }
    
    a
  5. p0W3RH311 commented on Feb 22, 2018

    @p0W3RH311
    Author

    Mark Kraus (@markekraus)

    PowerShell v6.0.0-rc.2
    Copyright (c) Microsoft Corporation. All rights reserved.
    
    https://aka.ms/pscore6-docs
    Type 'help' to get help.
    
    
    PS C:\Users\My\Desktop> & 'C:\Users\My\Desktop\TEST.PS1'
    The expression after '&' in a pipeline element produced an object that was not valid. It must result in a command n
    ame, a script block, or a CommandInfo object.
    At C:\Users\My\Desktop\TEST.PS1:25 char:6
    +    & $Value
    +      ~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : BadExpression
    
    
            write-host 'hello'
    
    
  6. markekraus commented on Feb 22, 2018

    @markekraus
    Contributor

    Heh.. that's weird. works if I do this:

    Test.ps1:

    $keyword = [System.Management.Automation.Language.DynamicKeyword]@{
        Keyword ="b"
        BodyMode = [Management.Automation.Language.DynamicKeywordBodyMode]::ScriptBlock
        NameMode = [Management.Automation.Language.DynamicKeywordNameMode]::NoName
    }
    [System.Management.Automation.Language.DynamicKeyword]::AddKeyword($keyword)
    
    . .\Test2.ps1

    Test2.ps1:

    function b 
    {
        param (
            [Management.Automation.Language.DynamicKeyword] $KeywordData,
            $Name,
            [ScriptBlock] $Value,
            $SourceMetaData,
            $InstanceName
        )
       & $Value
    }
    
    function a 
    {
       b 
      {
            write-host 'hello'
       }
    }
    
    a

    then run & .\Test.ps1

    When I was tasting this out I was doing it with the interactive console on VS Code and executing a block at a time. It appears to not work when used in the same execution block, but if you can separate it out it works. i dunno. I didn't even know this existed until Jason Shirk (@lzybkr)'s comment in this issue.

  7. lzybkr commented on Feb 22, 2018

    @lzybkr
    Contributor

    There's a reason it's not formally supported yet.

  8. rjmholt commented on Feb 23, 2018

    @rjmholt
    Collaborator

    Yes, the parser has rules in it for DynamicKeywords that let you do things like hang the braces, and #3169 extends that so you can nest keyword contexts, but there are still issues with making the keyword functionality robust and predictable enough and they require some work to solve.

  9. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  10. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  11. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  12. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-No ActivityIssue has had no activity for 6 months or moreWG-Languageparser, language semantics

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions