Are there any libraries or projects that implement prototypal inheritance for PSObjects in Powershell?
+1
A:
I don't think so since you can't even get a PSObject to identify itself as a custom type e.g.:
PS> $obj = new-object psobject
PS> $obj.psobject.TypeNames.Insert(0,'MyCustomType')
PS> $obj.psobject.TypeNames
MyCustomType
System.Management.Automation.PSCustomObject
System.Object
PS> $obj -is [MyCustomType]
Unable to find type [MyCustomType]: make sure that the assembly
containing this type is loaded.
However you can use Add-Type -TypeDefinition
to sprinkle C#-based class definitions, complete with inheritance, in your script. The thing with PowerShell it is still primarily a command line shell and scripting language. It doesn't have (and probably shouldn't have) all the features of a general purpose programming language like say Python.
Keith Hill
2010-09-08 13:40:33
"it is still primarily a command line shell and scripting language" you could have said this about most of the "scripting languages". Furthermore I don't see why you would make an object based scripting language without making it possible to do OO programming in it.
Willbill
2010-09-08 14:22:05
Python has its console interpreter but I don't consider that a shell. KornShell, Bash, CSH, 4NT - those are shells. Keep in mind that PowerShell targets system admins just as much (if not more so) than developers. You think most admins are going to need inheritance? Probably not but devs I'm sure would love to see it. :-) Don't get me wrong, I understand the desire to be able to declare types, derive from types and implement interfaces but it is a slippery slope. Folks already complain PowerShell is too complex with a huge surface area.
Keith Hill
2010-09-08 15:09:34
Back in the early days of .NET ECMA specs there was this notion of consumer and extender languages. Consumer languages can use .NET types but not create new types. I've always considered PowerShell one of those "consumer" languages.
Keith Hill
2010-09-08 15:11:37
Check out section 7.2.2 of the CLI (ECMA-335) spec: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-335.pdf FWIW, there are things PowerShell can't do which doesn't even make it a full-fledged consumer language e.g. it can't call generic methods.
Keith Hill
2010-09-08 15:17:09
I see you point, looks like I saw what was there in terms of accessing CLR types and misjudged it completely
Willbill
2010-09-09 13:41:02
PowerShell has so much capability (for a shell) it is pretty easy to find yourself wanting, sometimes expecting, even more. Been there myself. :-)
Keith Hill
2010-09-09 14:54:49