How can I call the super method for this in an imp...
# announcements
c
How can I call the super method for this in an implementation?
Example of where things go wrong
r
You’re doing weird stuff
😋 1
c
Thanks lol
h
If you had put snippets instead of pictures, somebody might be able to try it…
c
I can provide pastebins, one sec
m
There is no
super
in this context. Extension methods are static.
☝️ 1
r
I don’t see why you have extension functions there in the first place
c
r
I feel like you’re trying to use
T.foo()
instead of
foo(t: T)
like they’re the same thing. They’re not
👀 1
c
@MiSikora Yeah, is there any work around for this situation? @ribesg The reason is to be in the command's context for proper syntax while working with the command
r
foo(ctx: CmdContext) = with(ctx) { ... }
c
That wouldn't fix the situation tho?
m
If you want to leverage polymorphism it has to be a regular method on a type.
r
Just don’t use extension functions. That’s your problem.
c
Alright, thankz
r
It’s actually the first time I see the keyword
override
in front of an extension function lol
c
I actually do that pretty often surprisingly
r
Yeah don’t
I understand why it’s allowed by the language but you’re abusing it
c
What would be a proper use case?
r
I don’t see any, but I guess there are some or it wouldn’t be allowed.
I only ever define extension functions inside classes as a tool to wrap some bits of complex code I’m using multiple times, to make the rest of the code more readable
Never used an extension function in an interface. 99.9% of my extension functions are top level.
c
What's the .1%? 😋
g
I disagree that this is abusing of the feature, this is completely valid case, especially for DSL But not sure about polymorphic usage like in this case and calling super, but in general imo it is fine
r
Which lib uses this kind of extensions inside interfaces?
m
@ribesg I can't find an example library right now, but it's a common pattern when you want to restrict calls to a function inside some DSL-scope. Like this:
Copy code
dsl {
    "some string".someDSLFunction()
}
👍 1
r
Ok, I see why it could be useful in this case.
m
Now, the problem here is that you kinda would like to write this:
Copy code
val context = this
with(super) {
    context.executeWithChecks()
}
But it doesn't compile, because you can't use
super
as an expression.
g
There is no way to call super function with receiver, but there is workaround for your case: https://pl.kotl.in/2VwzUeTxf It’s not elegant, but works, but probably you should reconsider your type hierarchy, maybe just extract C.executeWithChecks() as extension function instead member function with receiver, or some other approach
one more way: use standard functions in Cmd and for DSL provide some wrapper like CmdScope