Re: Optional arguments to constructors
Does this remain the recommended way to deal with Optional constructor arguments? On Tuesday, October 13, 2009 at 8:21:39 PM UTC-5, Jesse Wilson wrote: > > > > On Oct 13, 1:10 pm, Martin Grajcarwrote: > > Do you think it's bad idea? > > Yeah, I don't think it pulls its weight. With the current solution, > you get to supply your own fallback value such as NoOpAirConditioner. > If we permitted optionality on a parameter-by-parameter basis, our > poor users would have to cope with null. -- You received this message because you are subscribed to the Google Groups "google-guice" group. To unsubscribe from this group and stop receiving emails from it, send an email to google-guice+unsubscr...@googlegroups.com. To post to this group, send email to google-guice@googlegroups.com. Visit this group at https://groups.google.com/group/google-guice. To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/4d3812e1-9e44-445b-a93d-d46741a3aef0%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: Optional arguments to constructors
Hi, this works, but it needs quite a lot of boilerplate code including a holder class which must be public in case I want to configure the Car in different package. If wonder if making Guice recognize an @Optional annotation would be worth the effort. I think it must be quite simple, it can make no harm, the only question is if I'm the only one who'd use it. Do you think it's bad idea? Regards, Maaartin. je...@swank.ca wrote: There's a workaround in the FAQ: http://code.google.com/p/google-guice/wiki/FrequentlyAskedQuestions How can I inject optional parameters into a constructor? Neither constructors nor @Provides methods support optional injection. To work-around this, you can create an inner class that holds the optional value: class Car { private final Engine engine; private final AirConditioner airConditioner; @Inject public Car(Engine engine, AirConditionerHolder airConditionerHolder) { this.engine = engine; this.airConditioner = airConditionerHolder.value; } static class AirConditionerHolder { @Inject(optional=true) AirConditioner value = new NoOpAirconditioner(); } } --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups google-guice group. To post to this group, send email to google-guice@googlegroups.com To unsubscribe from this group, send email to google-guice+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-guice?hl=en -~--~~~~--~~--~--~---
Re: Optional arguments to constructors
On Oct 13, 1:10 pm, Martin Grajcar grajc...@seznam.cz wrote: Do you think it's bad idea? Yeah, I don't think it pulls its weight. With the current solution, you get to supply your own fallback value such as NoOpAirConditioner. If we permitted optionality on a parameter-by-parameter basis, our poor users would have to cope with null. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups google-guice group. To post to this group, send email to google-guice@googlegroups.com To unsubscribe from this group, send email to google-guice+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-guice?hl=en -~--~~~~--~~--~--~---
Re: Optional arguments to constructors
Guice refuses to inject null by default, which is a very good thing. Java doesn't support constructor arguments with defaults, so you would have to create bindings yourself with the @Optional annotation. Also, you would also need to add the @Nullable to allow Guice to inject a null value. I'm not sure about how this will help though, it seems like the amount of work associated with this pattern would make it rather undesirable. Regards, Gary On Mon, Oct 12, 2009 at 2:48 PM, Martin Grajcar grajc...@seznam.cz wrote: Hi, is there no way to make contructor arguments optional? I know there're alternatives, but IMHO none of them is as good as this: - optional setter injection disadvantages: the field can't be final the setter may have no other use marking the setter as private triggers unused warning - optional field injection disadvantages: the field can't be final the field gets marked as final by my IDE (which I don't want to switch off) Maybe it can be done in other way, I don't know. The example below illustrates what I mean. class GuiceOptionalArgumentDemo { /** * I'd love if Guice would understand something like this. * If an argument annotated with Optional si not configured, null gets injected. */ �...@retention(RUNTIME) public @interface Optional {} static class SomeClass { �...@inject SomeClass(@Optional Integer count) { this.count = count==null ? 42 : count.intValue(); } public int getCount() { return count; } private final int count; } public static void main(String[] args) throws Exception { Guice.createInjector().getInstance(SomeClass.class); } } Regard, Maaartin. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups google-guice group. To post to this group, send email to google-guice@googlegroups.com To unsubscribe from this group, send email to google-guice+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-guice?hl=en -~--~~~~--~~--~--~---
Re: Optional arguments to constructors
Hi! Gary Pampara wrote: Guice refuses to inject null by default, which is a very good thing. I know and I agree. Sure, the @Optional annotation would imply @Nullable. It'd be acceptable for me to be forced to use both annotations, but if Guice could understand @Optional, it could automatically assume @Nullable. Java doesn't support constructor arguments with defaults, so you would have to create bindings yourself with the @Optional annotation. Also, you would also need to add the @Nullable to allow Guice to inject a null value. I'm not sure about how this will help though, it seems like the amount of work associated with this pattern would make it rather undesirable. I'm not sure if I understand you. I don't need default arguments values support in Java, actually not even in Guice, as the class could often provide it's own defaults for itself (just like in my example). The support in Java could be nice, but it's not really necessary, as I can do it with some more lines using overloaded constructors. But there can be only one constructor annotated with @Inject, so I see no easy way for doing things like this. The only thing needed here is a way to make Guice happy with an unbound dependance (just like @Nullable makes it happy with dependance bound to null). To me it looks like there're plenty uses for that, just look how much classes in JDK have (a sort of) optional arguments. Regards, Maaartin. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups google-guice group. To post to this group, send email to google-guice@googlegroups.com To unsubscribe from this group, send email to google-guice+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/google-guice?hl=en -~--~~~~--~~--~--~---