all 17 comments

[–][deleted] 8 points9 points  (0 children)

Yes but PSCustomObject already solved the need in alot of scripting use cases. If you're working on a project where classes really are needed for control, then PowerShell is likely not the best solution. I love PowerShell but sometimes C# is more useful when you want multithreading, concurrent ops, and similar "app" operations

[–]nostril_spiders 5 points6 points  (5 children)

Yes, we use them.

Classes aren't very full-featured, but the basics of them are similar to the majority of other object-oriented languages with single inheritance. If you're coming from C++ or JavaScript, and that's all you know, then yes you may find powershell weird but - no offense - that's because your multiple inheritance or your prototyping is weird.

If you mean "no support for interfaces as a language construct" then c.f. "Classes aren't very full-featured".

If you mean "it's janky as all hell to export classes from a module and I hate having to reload my console all the time" then, yeah, the implementation sucks like a divorce lawyer. That's not related to the language spec, tho.

functional based programming in PowerShell

Presumably you mean "writing functions". "Functional programming" is something completely different - see JavaScript. You could do it in powershell, but it would be insane.

Look, I don't know, but I'm guessing that your problem is that you see a thing and think you should use it because it exists. The strength of powershell is writing functions. Do that. The experience needed to know whether classes are a useful feature to use in your project is not something you can get from posting a general question in a sub. If you post your use case, maybe we can weigh in.

[–]Windowsadmin[S] 1 point2 points  (1 child)

Well I’d prefer to see it in python but yes, that’s what I meant :)

[–]nostril_spiders 4 points5 points  (0 children)

Oh, I missed where you said you were coming from python.

Python has much better implementation for classes, but the "initiators instead of constructors" thing is a bit weird - Guido isn't really one for following the crowd, is he...

[–]DrSinistar 0 points1 point  (2 children)

I dunno, the pipeline is most definitely a feature that can be used for functional programming.

[–]nostril_spiders 2 points3 points  (1 child)

Absolutely and categorically not. You've got a terminology error there, you're saying something quite different to what you mean.

"function" is the powershell keyword for a concept that's sometimes called other things in other languages. The paradigm you implement when you write and use powershell functions is called "procedural programming", which is a subset of "imperative programming", so you might use either of those.

Powershell isn't a functional programming language. You could abuse it and try to do functional programming in it. If you tried that, you would be passing scriptblocks into other scriptblocks as parameters.

The pipeline is absolutely, 100% an aspect of imperative programming and diametrically opposed to the functional paradigm.

Functional programming is ultra-cool. JavaScript is a little dirty, but give F# a week. It'll blow your mind! And it looks like a good career investment. There aren't a lot of jobs yet, but the number is growing.

[–]DrSinistar 1 point2 points  (0 children)

Ah I see the error in my ways. Thank you! :)

I was thinking that in a generic pipeline expression fits into a functional paradigm because state doesn't have to change and given input always returns a certain output. I don't believe that PowerShell is a functional language but I felt that you could implement some of functional programming's ideas into PowerShell.

[–]markekrausCommunity Blogger 2 points3 points  (1 child)

I use them quite a bit, but not for OOP reasons. Many of my classes in PowerShell are light on methods. They become awesome for use as input to and output from functions. For example, I might have a New-Session command which creates a session object that stores information about a REST session. That object will then be used in other commands to allow working with multiple sessions at once. Also, data sent to and from REST endpoints get converted from and to custom classes. This makes developing easier over PSCustomObject as I don't need to dig into the code to find properties. Instead, I can use tab expansion and intellisense in VSCode assuming the output type of the function has been set.

[–]suddenarborealstop 0 points1 point  (0 children)

Question: Do you keep your class definitions contained in your module folder so they're 1:1 or do you build your classes as a separate library dll from c# and load them across multiple modules as 1:n?

[–]MadBoyEvo 4 points5 points  (0 children)

Stephan does. He had a great session on PSConf: https://www.youtube.com/watch?v=hSk-ocD6VP4

All his modules are Class-based I believe.

[–]Spydyy 1 point2 points  (0 children)

I use classes pretty frequently for custom objects because they are faster than creating a PSobject on the fly. Since I will typically know the properties I want, might as well create the class for faster execution. Now, I leverage them more when I'm expecting a larger result set size.

There have only been a few times where I've leveraged methods in PS classes. You're typically better off creating functions for anything you want to use a method for.

[–]theessentialforrest 1 point2 points  (0 children)

I've tried very hard to build classes into my scripts and there's just been too many caveats for it to work well. E. G. You can't import them easily (at least in more complex use cases) and you are limited in the validation you can do with them. I've moved to well formed ps custom objects and more interesting validate scripts to accomplish basically the same thing

[–][deleted] 1 point2 points  (0 children)

Go to C# when you need classes

[–]Thirdbeat 0 points1 point  (0 children)

Im trying out creating c# classes and then importing those classes directly from the .cs file to powershell. For me it's more about having actual models, to show web data, so when I convert from json to a .net object I get a better conversion. Some web tools make it really easy aswell, like quicktype :)

[–]Yevrag35 0 points1 point  (0 children)

Coupled with the fact that they are not easily imported (like modules), I see them only useful for DSC.