Converting from java

How do I convert this Java to Kotlin

DataFetcher userDataFetcher = new DataFetcher() {
@Override
public User get(DataFetchingEnvironment environment) {
// environment.getSource() is the value of the surrounding
// object. In this case described by objectType
User value = EntityData.Companion.getUser(environment.getArgument(“id”)); // Perhaps getting from a DB or whatever
return value;
}
};

val userDataFetcher = object:  DataFetcher() {
    override fun get ...
}
1 Like

Thank you so much for this.

Also if you are converting java code to kotlin you should look at the java2kotlin converter that comes with intellij. It’s not perfect, but it should speed up conversion quite a bit.

1 Like

Unfortunately in this case it was giving me something strange that was not working, which is why I turned to this forum. Again thanks. Around how long did it take you to feel truly comfortable with kotlin? Do you code in kotlin as your main job?

Yeah, AFAIK there are some corner cases that don’t work. And improving the converter isn’t a focus of the kotlin team right now. I guess it works good enough for most cases and I think some manual work is good when converting code anyways. That way it’s easiser to spot subtle mistakes that a converter might make.

I kinda fell in love with kotlin the moment I started using it. Maybe took me few months to get fully comforable with it switching from java and C#. I try to use kotlin for everything I can, but my university is still using Java and C/C++ for most things so can’t use it for my “main job”(studying) :wink:

1 Like

I hope your job will allow you to use more kotlin. Yes I have also fallen in love with it, coming from C# and JavaScript