-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to specify an order for the Classes? #18
Comments
We specifically talked about this during design. The only correct answer is for PowerShell to fix this so the order doesn't matter. Anything else is a workaround. In my opinion, the simplest workarounds are the best: number your files to force the order. It's not only simple, it's easy and obvious (people who look at the code can see you deliberately ordered the files). In the worst case, we need to add an explicit The only other ordering that we follow is that the SourceDirectories are processed in order. If you really want to, you can use and abuse that to get anything you need (please don't). In fact, at design time I gave in to adding the "enum" folder (which I personally think is a bad idea, and won't use) because so many of the folks who were present were convinced that enums are different, and this would be enough for them to handle their dependencies. I would be happy to consider automatically determining the order at compile time if anyone wants to have a go at that, but right now I don't really think it's worth the effort (except that I'd love for that to be built in to PowerShell). |
Good for me. |
What do you think of adding a sort-of pre-compiler directive option, like a line at the top of the file:
Then we could just read the very first line and sort by that? I mean, it's still a hack, but it's a simple hack, and it doesn't require much parsing, nor affect line numbering. It would not affect the current solution if people want to number them instead, but doing it this way would allow your class files to be in alphabetical order in your IDE without a major impact. Either way, should we also add something where we attempt to load the module, and try to detect errors caused by bad order? |
Is this actually a problem? I just tried putting this in a class Computer {
[string]$Name
[Person]$Owner
[string]ToString() { return $this.Name }
}
class Person {
[string]$Name
[Computer]$Computer
[string]ToString() { return $this.Name }
}
function New-User {
[CmdletBinding()]
param([Parameter(Mandatory)]$UserName, $ComputerName)
$P = [Person]@{
Name = $UserName
}
$C = [Computer]@{
Name = $ComputerName
Owner = $P
}
$P.Computer = $C
$P
} That seems to work even in PowerShell 5 ... |
You're right, it is not a problem in a compiled PSM1. |
As you know Classes sometimes need to be imported or merged in a specific order.
I haven't found an elegant way of importing them in a specific order without prepending a number in the file name (which I'm not a fan of either).
e.g.:
I tend to prefer specifying the order in a PSD1 and/or sorting the files within the Classes folder for merging them in the PSM1, but that's a bit over-engineered:
Happy to go with file names (less code), if that's your intent.
The text was updated successfully, but these errors were encountered: