Call for feedback -- enhanced switch
Jeyvison Nascimento
jeynoronha at gmail.com
Tue Mar 5 20:28:17 UTC 2019
Hey all. I'd like to share a few impressions from what I saw about the
Enhanced switch.
The JEP is really good and it makes the code a lot cleaner. I don't know if
we still have time to suggest something but I'd like to suggest three
things to be added to it. I'm sorry if it was already discussed but I'm
pretty new on this list.
*Type Comparison when arg is Object*
Imagine we are calculating an insurance risk based on the type of the
vehicle we wanna get a quote. So we wanna cal different methods and perform
different operations if this vehicle is a boat, a car or a bus.
I know we can rely on String for that, adding the type as String attribute
or relying on *.getClass().getName()), but the former is error-prone and
later is very repetitive.
We can also use Enums for that, but again it becomes an instance attribute
and there are situations when we cannot use Enums( trust me, it's a thing).
We could have some code that looks like this:
int insuranceRisk = switch (vehicle){
case Boat -> 3 * analyzeManufacturingDate(vehicle); //vehicle casted to Boat
case Bus -> 2 * vehicle.numberOfPassengers(); // vehicle casted to Bus.
case Car -> analyzeSeatBeltsAndTires(vehicle); // vehicle casted to Car.
default -> 0;
};
We compare the type of the instance arg and autocast it at the right of
"case L".
*Calling default value when no value is returned from expression.*
TBH this one is not very clear to me if it's really necessary/good but I'll
leave it here if you guys have something to say about this. In resume,
there can be some situations when we wanna perform different operations but
return the same value and it can be a little repetitive.
We could have something like this:
int taxes = switch(taxes_type){
case TaxesType.FEDERAL_TAXES -> voidLogFederalTaxes();
case TaxesType.STATE_TAXES -> intCalculateStateTaxes();
case TaxesType.CITY_TAXES -> voidCallCityTaxesAPI();
default -> calculateCommonTaxes();
};
The first and third cases call a void method that performs a specific
operation. Of course, I could make then break and return
*calculateCommomTaxes()* but you can see that it can get repetitive(as in
code duplication) over time.
*Break to Break-with*
A minor sugestion. * Break value* and * break-with* *value* doesn't seem
very descriptive to me. Maybe *break-return value*?
*Resume*
In resume, all these suggestions are from someone that has no idea if it
was already discussed or the implementation cost of it but i'd like to hear
what you folks hate
to say about it :)
Thanks a lot.
Em seg, 4 de mar de 2019 às 07:24, Ben Evans <benjamin.john.evans at gmail.com>
escreveu:
> Hi Brian,
>
> Can you explain the urgency here?
>
> If this is going to be a permanent feature as of Java 13, the deadline
> for confirming that is surely months away, and I thought that one of
> the points of Preview Features was that the promotion to permanent was
> supposed to be a quick flick of the switch (no pun intended).
>
> On the other hand, Java 12 isn't even out yet! So no-one who isn't an
> avid early adopter, or subscribed to this group (& I posit that we're
> not exactly normal Java developers, for the most part) has tried this
> feature in anger.
>
> As Martijn pointed out, this feature has just landed in the nightly
> builds of IntelliJ - perhaps Trish or one of the other JetBrains folks
> can give us an idea of how many downloads they see for the nightlies?
> - that might give us some idea of how many developers have actually
> tried out the feature (although, of course, they are still a
> self-selecting early adopter group).
>
> Thanks,
>
> Ben
>
> On Fri, 1 Mar 2019 at 22:55, Brian Goetz <brian.goetz at oracle.com> wrote:
> >
> > We would like to promote enhanced switch to a permanent feature in Java
> 13. So far, the only change we are anticipating is:
> >
> > - change “break value” to “break-with value”.
> >
> > This is the time for people to try it out on their codebases (there’s
> even refactoring support in IDEA!) and provide feedback on what works, what
> doesn’t, and what was surprising.
> >
> >
>
--
Jeyvison Nascimento
More information about the amber-dev
mailing list