3

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.

Matthew S
  • 900
  • 3
  • 12
  • 26
  • You can also add ScriptMetod members to objects. – mjolinor Feb 20 '14 at 23:02
  • Related: http://stackoverflow.com/questions/18705158/powershell-create-a-class-file-to-hold-custom-objects/ – Anthony Neace Feb 20 '14 at 23:37
  • Readers from the future: As of Powershell 5.0 Classes are a first-class part of the language. No more kludges required. https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_classes?view=powershell-5.1 – TurtleZero Oct 24 '17 at 14:42

1 Answers1

4

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()
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • This seems to work until one tries to change e.g. `$Name` with a setter method. I guess because `$Name` is local to the method? How can I update a variable at "instance scope" via a method? – Adrian Frühwirth Sep 26 '16 at 10:24