Click here to Skip to main content
16,004,647 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Kotlin
class UpperCaseWriter(realWriter: Writer) : FilterWriter(realWriter) {

    override fun write(cbuf: CharArray, off: Int, len: Int) {
        val newBuff = CharArray(len)
        cbuf.copyInto(newBuff, destinationOffset = 0, startIndex = off, endIndex = off + len)
        newBuff.indices.forEach { n ->
            newBuff[n] = newBuff[n].uppercaseChar()
        }

        super.write(newBuff) // This should call FilterWriter's write method
    }//but it seems it calls the below write() function that takes str not chararray

    override fun write(str: String, off: Int, len: Int) {
        super.write(str.uppercase(), off, len) // but this is with different signiture?
    }
}

I am sorry for this basic question.
Is it because of delegation to realwriter(except for overriden methods ), if so why super?

What I have tried:

I thought it was overloading. But I am lost.
Posted
Updated 30-Jul-24 23:03pm
v2
Comments
Member 13798091 31-Jul-24 6:59am    
The problem is that when I delete uppercase() from this
super.write(str.uppercase(), off,
len)
It will not print the text uppercased?
And when I hit ctrl and click on write from this super.write(newbuf) it takes to me the write(str, off,
len) that is overriden bellow it?

I'm not a Kotlin expert, but the method signature doesn't match: your new method requires 3 parameters (CharArry, Int, and Int) but your call provides only one.

In most languages, overloaded methods are only called when the parameters match. I'd suspect Kotlin is the same.
 
Share this answer
 
Comments
Member 13798091 31-Jul-24 7:00am    
The problem is that when I delete uppercase() from this
super.write(str.uppercase(), off,
len)
It will not print the text uppercased?
And when I hit ctrl and click on write from this super.write(newbuf) it takes to me the write(str, off,
len) that is overriden bellow it?
If I understand Kotlin method resolution then unless the superclass has a method with the signature fun write(CharArray) then Kotlin resolves the method call based on the most specific match available which in this case is your override fun write(str: String, off: Int, len: Int) method.

So as OG said, you need to make the super.write call match a method signature that is present in the superclass.
 
Share this answer
 
Comments
Member 13798091 31-Jul-24 7:01am    
The problem is that when I delete uppercase() from this
super.write(str.uppercase(), off,
len)
It will not print the text uppercased?
And when I hit ctrl and click on write from this super.write(newbuf) it takes to me the write(str, off,
len) that is overriden bellow it?
M-Badger 1-Aug-24 4:17am    
Update your question with the write method (and any overloads) from the superclass then someone might be able to help
fun main() {
    UpperCaseWriter(FileWriter("log.txt")).use {
        it.write(
            """
            This is a test 
            another line 
            SOME UPPERCASE TEXT
        """.trimIndent())
    }
}
//I think here, it.write(str) calls from java API Writer.java:

public void write(String str) throws IOException {
    write(str, 0, str.length())}
//and this write(str, 0, str.length())
//calls my overriden fun write that is: 
    override fun write(str: String, off: Int, len: Int) {
        super.write(str.uppercase(), off, len)
    }
//as I am delegating to realwriter that it use all the methods except for the overriden ones, Am I right here?
 
Share this answer
 
Comments
M-Badger 19-Aug-24 10:22am    
Your original code calls super.write(newBuff) where newBuff is a CharArray. The method signature in the superclass is write (String str) so Kotlin will not call a write method in the superclass because there is no write method with a matching signature. Kotlin then goes looking for a write method with a matching signature which it finds in your derived class.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900