Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

'trap' creates a new scope. Why?

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 10-07-2006   #1 (permalink)
=?Utf-8?B?Um9tYW4gS3V6bWlu?=
Guest


 

'trap' creates a new scope. Why?

'trap' creates a new scope. Why? I do not want to say that it is bad but it
would be interesting to know the origins of such a decision and benefits for
a user.

So far I see just inconveniences and lack of logic because, IMHO, the code
in trap by its nature should deal with something in the same scope where
'trap' is.

--
Thanks,
Roman

My System SpecsSystem Spec
Old 10-09-2006   #2 (permalink)
James Truher
Guest


 

Re: 'trap' creates a new scope. Why?

I answered this in the other thread - so you can avoid searching, here it is


we definitely discussed both ways of having trap work. In the end, we
wanted to avoid the general pollution that we thought would occur if we had
the trap execute in the same scope of the function body, we also felt that
the unintended side-effects would also be harder to debug. Lastly, remember
that you can have any number of traps within a function. So far, the
examples have been pretty simple, but it can be much more complicated:

function badbad
{
trap [exception1] { ... }
trap [exception2] { ... }
trap [exception3] { ... }
trap [exception4] { ... }
trap [exception5] { ... }

....

}

The pollution caused by all these traps was felt to be more a hindrance than
a help. Especially since it's so easy to adjust variables in the parent
scope.

jim


--
--
James Truher [MSFT]
Windows PowerShell Development
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.

"Roman Kuzmin" <RomanKuzmin@discussions.microsoft.com> wrote in message
news:10DCE558-7139-4814-ADC8-A7121FF211E9@microsoft.com...
> 'trap' creates a new scope. Why? I do not want to say that it is bad but
> it
> would be interesting to know the origins of such a decision and benefits
> for
> a user.
>
> So far I see just inconveniences and lack of logic because, IMHO, the code
> in trap by its nature should deal with something in the same scope where
> 'trap' is.
>
> --
> Thanks,
> Roman



My System SpecsSystem Spec
Old 10-09-2006   #3 (permalink)
Roman Kuzmin
Guest


 

Re: 'trap' creates a new scope. Why?

"James Truher" wrote:
> ...


James, thank you for your remarks.

I could easily avoid pollution (only if I need!) on my own using simple
language capabilities to create nested scopes:

function badbad
{
trap [exception1] { &{<nested scope>} }
trap [exception2] { &{<nested scope>} }
trap [exception3] { &{<nested scope>} }
....

At the same time I could still have a choice to work with current scope:

trap [exception4] { <current scope> }
trap [exception5] { <current scope> }

It is nice to have a choice. With current implementation of 'trap' we don't
have a choice. Besides:

*) this is not intuitive, just listen to this forum, people look trapped by
this trap scope oddity;
*) the way to access parent scope is not 'easy' as it was said, it is rather
cumbersome (via Set-Variable cmdlet);
*) 'trap' is a part of language, it should not require cmdlets for its
usage.

Thanks,
Roman


My System SpecsSystem Spec
Old 10-09-2006   #4 (permalink)
Roman Kuzmin
Guest


 

Re: 'trap' creates a new scope. Why?

One more remark. I have written quite a number of PowerShell
scripts/functions and I have never yet used more than one trap in a scope.
Perhaps I am lazy. Perhaps I write mostly for my own work
automation/simplification and it is OK only for me. But I can hardly imagine
frequent examples with five traps in the same scope. It looks like a prelude
to a serious program.

I would not like to write programs in PowerShell, I would like to write
scripts, simple and clear as much as possible. But if one decides to use
five traps in a function in the same scope, then I believe he is experienced
enough to avoid pollution in his code: there are many easy ways, &{...} is
just one of them.

Thanks,
Roman


My System SpecsSystem Spec
Old 10-11-2006   #5 (permalink)
dreeschkind
Guest


 

Re: 'trap' creates a new scope. Why?

"Roman Kuzmin" wrote:

> But I can hardly imagine frequent examples with five traps in the same scope.


> But if one decides to use five traps in a function in the same scope, then I believe
> he is experienced enough to avoid pollution in his code: there are many easy ways, > &{...} is just one of them.


> 'trap' is a part of language, it should not require cmdlets for its usage.


IMHO these are all good points. Did you file this as a suggestion on connect?
Though I doubt that there will be any changes at this stage. :-|

--
greetings
dreeschkind
My System SpecsSystem Spec
Old 10-11-2006   #6 (permalink)
Roman Kuzmin
Guest


 

Re: 'trap' creates a new scope. Why?

"dreeschkind" wrote:
> Did you file this as a suggestion on connect?


https://connect.microsoft.com/feedba...4294&SiteID=99

--
Thanks,
Roman


My System SpecsSystem Spec
Old 10-16-2006   #7 (permalink)
James Truher
Guest


 

Re: 'trap' creates a new scope. Why?

correct, we won't be changing this behavior at this point

--
--
James Truher [MSFT]
Windows PowerShell Development
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.

"dreeschkind" <dreeschkind@discussions.microsoft.com> wrote in message
news:AE5A5E24-7D01-403E-8E66-2303D0ADBF6B@microsoft.com...
> "Roman Kuzmin" wrote:
>
>> But I can hardly imagine frequent examples with five traps in the same
>> scope.

>
>> But if one decides to use five traps in a function in the same scope,
>> then I believe
>> he is experienced enough to avoid pollution in his code: there are many
>> easy ways, > &{...} is just one of them.

>
>> 'trap' is a part of language, it should not require cmdlets for its
>> usage.

>
> IMHO these are all good points. Did you file this as a suggestion on
> connect?
> Though I doubt that there will be any changes at this stage. :-|
>
> --
> greetings
> dreeschkind



My System SpecsSystem Spec
Old 10-16-2006   #8 (permalink)
Maximilian Hänel
Guest


 

Re: 'trap' creates a new scope. Why?

Hi James,

> correct, we won't be changing this behavior at this point


So you will never change this behavior? Or is backwards compatibility
not an issue for PS?

cu

Max
My System SpecsSystem Spec
Old 10-18-2006   #9 (permalink)
James Truher
Guest


 

Re: 'trap' creates a new scope. Why?

correct - we are definitely concerned with backward compatibility and we've
been thinking alot about backward compatibility and our ability to innovate.
For now, consider that this behavior will never change.

jim

--
--
James Truher [MSFT]
Windows PowerShell Development
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.

"Maximilian Hänel" <ngSpam@smjh.de> wrote in message
news:ulKLONV8GHA.4408@TK2MSFTNGP02.phx.gbl...
> Hi James,
>
>> correct, we won't be changing this behavior at this point

>
> So you will never change this behavior? Or is backwards compatibility not
> an issue for PS?
>
> cu
>
> Max



My System SpecsSystem Spec
Old 10-18-2006   #10 (permalink)
Maximilian Hänel
Guest


 

Re: 'trap' creates a new scope. Why?

Thanks for clarifying!

cu

Max
My System SpecsSystem Spec
Closed Thread

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to trap exceptions? schadek PowerShell 0 05-28-2008 09:54 PM
How to Stop Trap Peter Sun PowerShell 2 02-15-2008 01:10 PM
trap scope Bob Butler PowerShell 9 10-27-2007 08:41 PM
Trap about Gwmi HungYuWu PowerShell 2 03-21-2007 08:32 PM
Can TRAP be redeclared? Maxim Maslov PowerShell 5 12-22-2006 08:14 AM


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51