How to simplify nested ifs

rumbu rumbu at rumbu.ro
Tue Mar 13 13:13:07 UTC 2018


On Tuesday, 13 March 2018 at 12:23:06 UTC, Ozan Süel wrote:
> Hi
>
> I have a construction like the following
>
> if (source) {
>   if (source.pool) {
>     if (source.pool.repository) {
>       if (source.pool.repository.directory) {
> 	if (source.pool.repository.directory.users) {
> 	  // do something
>
> Any chance to simplify this nested ifs?
> I know some languages has a way like.
>
> if (source?pool?repository?directory?users) // do something
>
>
> Similar ways in D?
>
> Thanks and Regards, Ozan

You need the null conditional operator which is not available in 
D.

A library solution can be found there: 
https://forum.dlang.org/post/mailman.2562.1403196857.2907.digitalmars-d@puremagic.com

SafeDeref(source).pool.repository.directory.users


More information about the Digitalmars-d-learn mailing list