Comments (11)
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.
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.
@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.
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.
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.
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.
Sweet! I'll keep watching then 👍
from otto.
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.
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.
I vote for not supporting. This can come later if we determine it's really, really needed.
from otto.
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)
- duplicate entry: com/squareup/otto/AnnotatedHandlerFinder.class HOT 1
- NoClassDefFoundError when running in Android OS below 5.0. HOT 3
- subcriber received two same event HOT 2
- Please include the deprecated notice in the info website
- Examples HOT 1
- Please add notice to website about otto deprication HOT 1
- How to customize ThreadEnforcer HOT 4
- Using privitives in callback haven't invoked it. HOT 3
- Link for Migrating to RxJava is dead. HOT 1
- Private EventHandler makes it impossible to override Bus#dispatch HOT 1
- OnEvent not calling in navigationdrawer fragment
- otto with JavascriptInterface in api < 17 HOT 1
- Ship proguard rules in the aar? HOT 1
- A new call to post in a subscriber does not call its subscribers until the first call is completed HOT 1
- Reason for its name HOT 1
- android.database.sqlite.SQLiteException: no such table HOT 4
- Update for Gradle 4 and Bus doesnt work more. HOT 3
- I can't receive the event HOT 1
- java.lang.RuntimeException: Could not dispatch event: class abc to handler
- java.lang.RuntimeException: Could not dispatch event HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from otto.