From ca78d2f6469edad19b57feb87a395021483649df Mon Sep 17 00:00:00 2001 From: Jake Bolton Date: Fri, 29 Dec 2023 15:39:20 -0600 Subject: [PATCH] new class ctor example --- .../Classes-StaticMethodDeDuplicatesCode.ps1 | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Pwsh/Types/Classes-StaticMethodDeDuplicatesCode.ps1 diff --git a/Pwsh/Types/Classes-StaticMethodDeDuplicatesCode.ps1 b/Pwsh/Types/Classes-StaticMethodDeDuplicatesCode.ps1 new file mode 100644 index 0000000..209469f --- /dev/null +++ b/Pwsh/Types/Classes-StaticMethodDeDuplicatesCode.ps1 @@ -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