How to simplify nested ifs

rikki cattermole rikki at cattermole.co.nz
Tue Mar 13 12:25:43 UTC 2018


On 14/03/2018 1:23 AM, 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

if (source !is null &&
	source.pool !is null &&
	source.pool.repository !is null &&
	source.pool.repository.directory !is null &&
	source.pool.repository.directory.users !is null) {
	//...
}

That is what I'd do.


More information about the Digitalmars-d-learn mailing list