reduce condition nesting

Andrey andrey at kabylin.ru
Thu Nov 23 08:46:00 UTC 2017


On Thursday, 23 November 2017 at 08:27:54 UTC, Andrea Fontana 
wrote:
> On Thursday, 23 November 2017 at 05:19:27 UTC, Andrey wrote:
>> Hello, is there way to reduce this condition:
>>> if (c1) {
>>>     foo();
>>> } else {
>>>     if (c2) {
>>>         bar();
>>>     } else {
>>>         if (c3) {
>>>         ...
>>>         }
>>>     }
>>> }
>>
>> for instance in kotlin it can be replace with this:
>>> when {
>>>     c1 -> foo(),
>>>     c2 -> bar(),
>>>     c3 -> ...
>>>     else -> someDefault()
>>> }
>
> if (c1) foo()
> else if (c2) bar();
> else if (c3) ...
> else someDefault();
>
> ?

haha, yes you are right, sorry for stupid question, I recently 
began to study Kotlin and noticed than `when` is a great feature )


More information about the Digitalmars-d-learn mailing list