Reputation: 1040
I have used closures in Powershell to create classes with static and instance methods. Is there a better way to do this?
Create and object with a static "method".
function person_get_type {
return 'Person'
}
function make_person {
param
(
[parameter(mandatory=$true)][string]$name)
$properties = @{'name'=$name;'get_type'=$null}
$person = New-Object –TypeName PSObject –Prop $properties
$person.get_type = ${function:person_get_type}
return $person
}
$person = make_person -name 'James'
$type = $person.get_type.InvokeReturnAsIs()
Write-Host $type
Create an object with an instance "method".
function dog_get_type {
return 'Dog'
}
function dog_make_get_name{
param
(
[Parameter(Mandatory=$true)][System.Management.Automation.PSObject]$self
)
return {return $self.name}.GetNewClosure()
}
function dog_make_set_name{
param
(
[Parameter(Mandatory=$true)][System.Management.Automation.PSObject]$self
)
return {param([parameter(mandatory=$true)][string]$name) $self.name = $name}.GetNewClosure()
}
function make_dog {
param
(
[parameter(mandatory=$true)][string]$name
)
$properties = @{'name'=$name;'get_type'=$null;'get_name'=$null;'set_name'=$null}
$dog = New-Object –TypeName PSObject –Prop $properties
$dog.get_type = ${function:dog_get_type}
$dog.get_name = dog_make_get_name -self $dog
$dog.set_name = dog_make_set_name -self $dog
return $dog
}
$dog = make_dog -name 'Spot'
$name = $dog.get_name.InvokeReturnAsIs()
Write-Host $name
$dog.set_name.Invoke("Paws")
$name = $dog.get_name.InvokeReturnAsIs()
Write-Host $name
$stuff = @($person,$dog)
foreach($thing in $stuff) {
Write-Host $thing.get_type.InvokeReturnAsIs()
}
I've see where it's possible to use this approach:
$object = New-Module -AsCustomObject -ScriptBlock {...}
But I don't see that's possible to create instance methods using this approach.
Upvotes: 3
Views: 798
Reputation: 201592
Instance methods should be easy using New-Module
. Your instance fields are the top level variables in the scriptblock e.g.:
$sb = {
param($theName,$theAge,$theBreed)
$Name = $theName
$Age = $theAge
$Breed = $theBreed
$global:NumDogs++
function Description {
"Dog named $Name, age $Age, breed $Breed"
}
function get_NumDogs {
"Total number of dogs is $NumDogs"
}
Export-ModuleMember -Variable Name,Age,Breed -Function Description,get_NumDogs
}
$dog1 = New-Module $sb -AsCustomObject -ArgumentList 'Mr. Bill',1,'Jack Russell'
$dog1.Name
$dog1.Age
$dog1.Description()
$dog1.get_NumDogs()
$dog2 = New-Module $sb -AsCustomObject -ArgumentList Fluffy,3,Poodle
$dog2.Name
$dog2.Age
$dog2.Description()
$dog2.get_NumDogs()
Upvotes: 4