Coder Social home page Coder Social logo

Generic producers about otto HOT 11 CLOSED

square avatar square commented on June 10, 2024
Generic producers

from otto.

Comments (11)

JakeWharton avatar JakeWharton commented on June 10, 2024

At runtime T is basically just Object which is why you won't see it producing events for whatever generic type you specify.

from otto.

romainpiel avatar romainpiel commented on June 10, 2024

I see so there is no way to know what's returned by produceEvent(), fair enough. It was just an idea, no worries.

from otto.

edenman avatar edenman commented on June 10, 2024

@JakeWharton could we do this when codegen is added, since it happens at compile time? I'm assuming the annotation processor gets generic type information...ignore me if it doesn't.

from otto.

pyricau avatar pyricau commented on June 10, 2024

The annotation processor gets generic type information. It get can as much information as you can get by reading the java code (except the code within methods of course).

When reading this:

public class MyClass<T> {

    T lastEvent;

    @Produce
    public T produceEvent() {
        return lastEvent;
    }
}

I don't see any information about the kind of object this might produce. Of course, if we now create a MySubclass extends MyClass<String>, that's another story..

from otto.

romainpiel avatar romainpiel commented on June 10, 2024

Actually that's exactly my use case. I'd like to implement stuff in my generic class that will be inherited by subclasses with types.
Ideally it would be nice if I'd have to skip this workaround:

public class MySubclass extends MyClass<String> {

    String lastEvent;

    @Produce
    public String produceEvent() {
        return lastEvent;
    }
}

(ie instantiating my producer in each subclass)

from otto.

pyricau avatar pyricau commented on June 10, 2024

This won't be possible with the current Otto version because we do not walk the class hierarchy.

With 2.0 we will switch to code generation and take parent classes into account. I know this use case is technically feasible, I don't know yet how easy it's going to be.

from otto.

romainpiel avatar romainpiel commented on June 10, 2024

Sweet! I'll keep watching then 👍

from otto.

swankjesse avatar swankjesse commented on June 10, 2024

We have the type resolution technology, but I don't think it's worth supporting. To get this really right you'd need to make register() take a type argument, which I find rather ugly.

from otto.

pyricau avatar pyricau commented on June 10, 2024

I don't think so.

public class MyClass<T> {

    T lastEvent;

    @Produce
    public T produceEvent() {
        return lastEvent;
    }
}
public class MySubclass extends MyClass<String> {}

if I register an instance of MySubclass, I do not need any type argument, we can already determine (both at compile time and runtime) that it's going to produce a String.

I'm not sure there's enough value to implement this though.

from otto.

JakeWharton avatar JakeWharton commented on June 10, 2024

I vote for not supporting. This can come later if we determine it's really, really needed.

from otto.

fred8 avatar fred8 commented on June 10, 2024

Hi all,
Just coming on this thread now. I think it would be interesting to add it. I have a bunch of events that are basically very close and I would like very much to be able to create a generic one.
Anyway to submit a pull request?

Thank you!

from otto.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.