powershellclasscompilationinterpreterpowershell-module

Understanding 'Powershell Runtime' and 'PowerShell Compile Time', Optimal method for sharing a class across multiple modules and scripts


So I finally finished building a class/type but now I am stumped on how to actually deploy it at the places I need. The class is supposed to provide a general purpose type, called Filmcode. I have a number of modules that I want to utilise this type with, as well as some some stand alone scripts.

I have been following the excellent book, Windows PowerShell in action (its actually PowerShell 6) by Bruce Payette and Richard Siddaway. Chapter 19 focuses on classes, and he talks about this importing subject:

19.4. Classes, modules, using, and namespaces

Now you know a lot about classes, but you still need to see how they’re organized for use and reuse. The fundamental element of reuse is, as always, the PowerShell module. You’ll organize your classes into modules and then use those modules in your scripts. The difference comes in how you use those modules. This is where another significant difference with classes shows up. Whereas most things in PowerShell are resolved at runtime, PowerShell classes are processed at compile time. When you want to get all the type-checking benefits that classes provide, particularly IntelliSense support, it’s necessary for PowerShell to know about classes ahead of runtime.


He talks about "most things in PowerShell are resolved at runtime" and "PowerShell classes are processed at compile time". I am so confused by how these terms are being used here.

But PowerShell is a interpreted language and I am using native Powershell code, so what is being compiled? Even then, if I just wanted to follow his guide, When does this "compile time" step occur? Is it when I launch the pwsh.exe process? or when I import the module, such as using it or manually importing it?

I think if I just understood "runtime" and "compile time", as they are being used in this book, and when they occur. I would know how to go about things.

I of course, can just paste the raw code for the class on the bottom of every ".ps1" or ".psm1" but this is not ideal, I want to learn how to do it the proper way

I searched for these two keywords but turned up with nothing relevant with regards to Powershell. Any insight would be most wellcome.

I am on Powershel 7.4


Solution


  • Implications of the above, limitations as of PowerShell 7.4:


    [1] Import-Module long predates the v5+ class support, and supports exporting only functions, aliases, and variables. using module invariably exports class definitions, and given that PowerShell class definitions lack C#-style access modifiers such as private, invariably exports all of them. However, while public .NET types (which is what PowerShell classes invariable are) are normally seen session-wide, and even though using module imports a module session-globally too, its PowerShell class definitions are visible only to the using module caller (and its descendent scopes).