Skip to content

Commit

Permalink
new class ctor example
Browse files Browse the repository at this point in the history
  • Loading branch information
ninmonkey committed Dec 29, 2023
1 parent 358dc3d commit ca78d2f
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Pwsh/Types/Classes-StaticMethodDeDuplicatesCode.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Vehicle {
[string]$Name = 'none'

Vehicle () {
# this works, instance is no longer assigned to 'none'
$this = [Vehicle]::Spawn('default')
}
Vehicle ( [string]$Name ) {
$this.Name = $Name
}
[void] Transform ( [string]$Name ) {
# this does not mutate the caller's state,
# because that's not allowed (outside of ctors)
$this = [Vehicle]::Spawn( $Name )
}
static [Vehicle] Spawn ( [string]$Name ) {
return [Vehicle]::New( $Name )
}
}

[Vehicle]@{} # Name: none
[Vehicle]::new($null) # Name: StrEmpty
[Vehicle]::new() # Name: none
[Vehicle]::new('blank') # Name: blank
[Vehicle]@{ Name = 'Car' } # Name: Car
[Vehicle]::Spawn('batmobile') # : Batmobile

[Vehicle]::Spawn('batmobile').Transform('cat') # emits null


$t = [Vehicle]::Spawn('batmobile') # Name: Batmobile
$t.Transform('yeti') # Name: Batmobile
# because

0 comments on commit ca78d2f

Please sign in to comment.