Best channel I've found, though I am not just gett...
# getting-started
l
Best channel I've found, though I am not just getting started with Kotlin --- does Kotlin not support overriding a function that accepts `String`s with one that accepts
CharSequence
s?
Is this really the best solution?
h
It's more like getting started with this Slack 🙂 > does Kotlin not support overriding a function that accepts `String`s with one that accepts
CharSequence
s? Of course not. A String is a CharSequence, but not every CharSequence is a String.
l
@hho but Java does allow this. Plus, every
String
is a `CharSequence`: I should be able to have my override accept more than required.
h
You're right, I was thinking of return values.
r
You might need to give a more clear example because what you're describing is invalid in Java too:
Copy code
class Foo {
	public void doThing(String s) { }
}

 class Bar extends Foo {
	@Override
    public void doThing(CharSequence s) { }
}

error: method does not override or implement a method from a supertype
l
@Robert Williams make
Foo.doThing
abstract, there's no error.
It is valid in Java
I checked before sending here
r
Copy code
error: Bar is not abstract and does not override abstract method doThing(String) in Foo
What Java/ JVM version are you using?
l
@Robert Williams 17
r
And are you talking about the same example as I posted or are you talking about the return type?
l
@Robert Williams please read above: I'm not talking about the return type
e
this Java code does not compile
☝️ 1
l
Copy code
public interface Test {

    void test(String x);

    public class Impl implements Test {
        @Override
        public void test(CharSequence x) {

        }
    }
}
Produces no error for me.
e
using the same source file, I get failures in every Java version from 8 to 23
👆 1
l
what
v
Maybe it produces no error because you don't compile it? For example because you paste it in a Java file that is not in a source root of the IDE and thus you only have basic code highlighting and assumed it works?
l
@Vampire nope, syntax highlighting did work, but compiler errors didn't cause only Kotlin/JVM was installed, not Java, which for whatever reaosn still enables the Java source set
But yeah it shouldn't work I confused myself somehow
e
assuming you're using the JVM target in Kotlin Multiplatform… fair, KGP doesn't help with the confusion. https://youtrack.jetbrains.com/issue/KT-53812 should improve things in the future
v
@Vampire nope, syntax highlighting did work
Basic syntax highlighting or full IntelliSense?
l
Full intellisense
Everything but errors
v
Well, if that really is true, maybe you should report an error to JetBrains 🙂
l
Yup
e
to be fair, there is a case where the opposite happens (sorta)
to Java 1.4 (and earlier) which predate generics, it looks like it should not work
but the Java compiler creates a synthetic bridge method (and so does Kotlin)
l
@ephemient can you resend that it's not rendering correctly
Ok it does now
Yeah that's why I got confused, cause it does work for generics
I'm just so used to functional programming, the one time I'm doing something OOP adjacent, just forgor
e
it's also the other way around
l
@ephemient what do you mean?
e
it's "overriding" with a narrower type, not a wider one. the compiler inserts a synthetic bridge which performs a cast
l
You mean, for generics?
e
yes
l
oh ic