question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PS C:\> $PSVersionTable.PSVersion

Major  Minor  Patch  PreReleaseLabel BuildLabel
-----  -----  -----  --------------- ----------
7      1      0

Hi Guys !!

here an example to demonstrate the new feature

first create base class


PS C:\> class foo {
>>        $say = 'hello'
>>       [string] sayhello($name) {
>>          return '{0} {1}' -f $this.say, $name
>>        }
>> }

work well


PS C:\> [foo]::new().sayhello('world')
hello world

create a child class with the same method ‘sayhello’ to overid the base method class


PS C:\> class bar : foo {
>>         [string] sayhello($name) {
>>             return "power${name}"
>>         }
>>    }
PS C:\>

work well



PS C:\> [bar]::new().sayhello('shell')
powershell

but sometimes we need to append the result to method “sayhello” in base class and the method “sayhello” in our child class… like in ruby with “super” keyword

this example demonstate the behaviour missing in powershell. in this example i use “super” keyword


PS C:\> class bar : foo {
>>         super [string] sayhello($name) {
>>             return "power${name}"
>>         }
>>    }

PS C:\> [bar]::new().sayhello('shell')
hello shell
powershell

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

3reactions
SeeminglySciencecommented, Feb 14, 2021

the problem with your ideas

Just to be clear, the first example isn’t an idea, that’s how the PowerShell team decided to implement that functionality (side note: I can kinda see why. $base would have been a pain to implement with how the binder works, and since it probably doesn’t matter for DSC it likely wasn’t a big priority). Like that example currently works.

$base would have been nice, but implementing it today may not be feasible without breaking changes.


As for super, that’s sort of confusing with a return value. It always just adds them together? So if the method returns int, the parent returns 5 and the child returns 5 then the invocation returns 10?

3reactions
zett42commented, Feb 13, 2021

Currently you can call a method of the base class like this, but it requires an ugly type cast (see about_Classes):

class bar : foo {
    [string] sayhello($name) {
        return ([foo] $this).sayhello($name) + "`npower${name}"
    }
}

Output:

hello shell
powershell

I would prefer to call the base class without having to repeat its name and without casting, similar to C#'s base keyword:

class bar : foo {
    [string] sayhello($name) {
        # Currently not possible!
        return $base.sayhello($name) + "`npower${name}"
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Metamorphism
Metamorphism is the transformation of existing rock (the protolith) to rock with a different mineral composition or texture. Metamorphism takes place at ...
Read more >
Types of Metamorphism
In geology this refers to the changes in mineral assemblage and texture that result from subjecting a rock to conditions such pressures, ...
Read more >
What are metamorphic rocks? | U.S. Geological Survey
Metamorphic rocks form when rocks are subjected to high heat, high pressure, hot mineral-rich fluids or, more commonly, some combination of these factors....
Read more >
Metamorphic Rocks
Metamorphism occurs because rocks undergo changes in temperature and pressure and may be subjected to differential stress and hydrothermal ...
Read more >
What are the causes of metamorphism?
Metamorphism is a process that changes pre existing rocks into new forms because of increases in temperature, pressure, and chemically active fluids. The...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found