Feedback wanted: switch expression typing
Ali Ebrahimi
ali.ebrahimi1781 at gmail.com
Sat Mar 31 07:26:08 UTC 2018
Hi,
echoing Dan,
Currently ?: is not consistent with if-else stats and that is may cause
hidden bugs or surprising results if refactored via-versa.
zbox = null;
test = false;
Boolean x = test ? z : zbox; // NPE
Boolean x;
if(test)
x = z;
else
x = zbox; // OK
What if want refactor multi-if-else branches to expression switches, should
be consistent with ?: OR if-else. Absolutely later.
I think in long term ?: should be fixed.
On Sat, Mar 31, 2018 at 6:14 AM, Dan Smith <daniel.smith at oracle.com> wrote:
> > On Mar 30, 2018, at 10:54 AM, Remi Forax <forax at univ-mlv.fr> wrote:
> >
> > I do not see (B) as sacrifying the consistency because the premise is
> that an expression switch should be consistent with ?:
> >
> > But an expression switch can also be modeled as a classical switch that
> returns it's value to a local variable.
> >
> > int a = switch(foo) {
> > case 'a' -> 2;
> > case 'b' -> 3;
> > }
> > can be see as
> > int a = $switch(foo);
> > with
> > int $switch(char foo) {
> > case 'a': return 2;
> > case 'b': return 3;
> > }
>
> I mean, sure, this is another way to assert "switches in assignment
> contexts should always be poly expressions".
>
> But it's just as easy to assert "conditional expressions in assignment
> contexts should always be poly expressions".
>
> int a = test ? 2 : 3;
> can be seen as
> int a = $conditional(test);
> with
> int $conditional(boolean test) {
> if (test) return 2;
> else return 3;
> }
>
> Those are probably good principles. But if we embrace them, we're doing
> (C).
>
> —Dan
--
Best Regards,
Ali Ebrahimi
More information about the amber-spec-observers
mailing list