Friday, January 1, 2021

PowerShell: Constructor and Method Overloading

With PowerShell's support for classes came the support method and constructor overloading. The past few posts have shown instances of method and constructor overloading that will be reviewed.
 
The parameters passed to an overloaded method or constructor determines which implementation is invoked as the parameter types in a method/constructor signature. The names of the parameters have no bearing on the signature of a method/constructor. The return value of a method does not affect which overloaded version of a method is invoked as the return value is not part of a method's signature. It is is not possible for PowerShell to distinguish between the following two versions of the GetValue method (implemented at line 4 and line 8) as they have the same parameters (not parameters):


The error message generated in Visual Studio Code is as follows:


Constructor Overloading

The post PowerShell: Enumerations implemented a class, SimpleLogEntry, that contained two constructors (constructor overloading) implemented at line 28 and line 34 respectively:


The first overloaded constructor (line 28) is unique based on the parameter signature:'

[LogLevel] $logLevel, [string] $message

The first overloaded constructor (line 23) is unique based on  the parameter signature:

[string] $logLine

The ErrorRecordExtension class was introduced in PowerShell: Handling/Logging Exceptions. The version of the ErrorRecordExtension class shown below contains three constructors but what appear to be only two signatures:


The instance constructors at line 16 and line 19 clearly have different parameters and hence different signatures. The third constructor, line 12, is a static constructor that like the instance constructor at line 16 takes no parameters are arguments. The static constructor is called the first time a method, constructor, or property is accessed from the ErrorRecordExtension type. The static constructor at line 12 cannot be explicitly invoked. The instance constructor at line 16 is explicitly invoked using the new keyword in PowerShell.  The fact that the instance and static constructor have the same parameters (none) is not case of overloading as they are different types of constructors.

Method Overloading

The post PowerShell: Handling/Logging Exceptions demonstrated method overloading as is shown with the GetException method implemented at line 77 and line 92 which are implemented in the ErrorRecordExtension class:


The signatures of each implementation of the GetException methods are different. The implementation at line 92 takes no parameters and the implementation at line 77 takes a lone parameter of type [Exception]. The GetException method implemented at line 92 is method invoked on an instance of type ErrorRecordExtension. The GetException method implemented at line 77 is a static method. Whether a method is static or not has no affected on the method's signature. 

Handling Multiple Overloads: Calling a Specific Method

Microsoft's documentation on PowerShell methods, About methods, discusses an interesting (albeit rare) case of method overloading which is presented as follows:

 


No comments :

Post a Comment