Remix.run Logo
mdaniel 8 days ago

Similarly unsurprisingly, with its BEGIN friend https://docs.ruby-lang.org/en/3.3/syntax/miscellaneous_rdoc....

In the spirit of "what's old is new again," PowerShell also has the same idea, and is done per Function with "begin", "process", "end", and "clean" stanzas that allow setup, teardown, for-each-item, and "finally" behavior: https://learn.microsoft.com/en-us/powershell/module/microsof...

mananaysiempre 8 days ago | parent [-]

Oh, that’s an interesting take. I’ve long been looking for newer developments on Awk’s clause structure, and this seems like an interesting take (though I’m unclear on whether I can have multiple begin/end clauses, which are the best thing about Awk’s version). It also finally connects this idea to something else in my mind—specifically advice[1] and CLOS’s :before/:after/:around methods[2]. (I guess Go’s defer also counts?)

[1] https://en.wikipedia.org/wiki/Advice_(programming)

[2] https://gigamonkeys.com/book/object-reorientation-generic-fu...

mdaniel 8 days ago | parent [-]

It seems not:

Given:

    function Fred {
        begin {
            echo "hello from begin1"
        }
        begin {
            echo "hello from begin2"
        }
        process {
            echo "does the magic"
        }
    }
    $bob = @("alpha" "beta")
    $bob | Fred
Then

    $ pwsh fred.ps1
    ParserError: /Users/mdaniel/fred.ps1:5
    Line |
       5 |      begin {
         |      ~~~~~~~
         | Script command clause 'begin' has already been defined.