You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Makes sure the optional's underlying collection isn't empty. If so, returns Unspecified.
publicstaticOptional<TCollection>NotEmpty<TCollection>(thisOptional<TCollection>val)whereTCollection:IEnumerable{TCollectionvalue=val.GetValueOrDefault();if(valueis not null){// adopted from Linq Enumerable<T>.Any,// but for non-generic enumerableIEnumeratorenumerator=value.GetEnumerator();boolhasValue=enumerator.MoveNext();if(enumeratorisIDisposabledisposable){disposable.Dispose();}if(hasValue){returnval;}}returnOptional<TCollection>.Unspecified;}publicstaticOptional<TCollection>NotEmpty<TCollection,T>(thisOptional<TCollection>val)whereTCollection:IEnumerable<T>{return(val.GetValueOrDefault()?.Any()??false)?val:Optional<TCollection>.Unspecified;}
Optional<T>.Map<U>
A utility method to map the value in the Optional to another. This removes a lot of IsSpecified checks inside API code.
Take for example #2724, where there was an extra check due to IsSpecified
// ChannelHelper.cspublicstaticasyncTask<Model>ModifyAsync(IGuildChannelchannel,BaseDiscordClientclient,Action<GuildChannelProperties>func,RequestOptionsoptions){varargs=newGuildChannelProperties();func(args);varapiArgs=newAPI.Rest.ModifyGuildChannelParams{Name=args.Name,Position=args.Position,CategoryId=args.CategoryId,Overwrites=args.PermissionOverwrites.IsSpecified?args.PermissionOverwrites.Value.Select(overwrite =>newAPI.Overwrite{TargetId=overwrite.TargetId,TargetType=overwrite.TargetType,Allow=overwrite.Permissions.AllowValue.ToString(),Deny=overwrite.Permissions.DenyValue.ToString()}).ToArray():Optional.Create<API.Overwrite[]>(),Flags=args.Flags.GetValueOrDefault(),IconEmoji=args.IconEmoji.IsSpecified?args.IconEmoji.ValueisEmoteemote?newAPI.Emoji{Id=emote.Id,Name=emote.Name}:args.IconEmoji.GetValueOrDefault(null)isEmojiemoji/*GetValueOrDefault not required*/?newAPI.Emoji{Name=emoji.Name}:null:Optional<API.Emoji>.Unspecified,};returnawaitclient.ApiClient.ModifyGuildChannelAsync(channel.Id,apiArgs,options).ConfigureAwait(false);}
When using Optional<T>.Map, this code gets much easier to read:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Motivation
Working with
Optionalinside the library is already a pain withIsSpecifiedchecks, and passing in "null" values intoOptional's underlying value.Optional.CreateFromNullable<T>Creates an optional from the specified nullable value, where it returns
Optional<T>.Unspecifiedif the value isnull.Optional.NotEmptyMakes sure the optional's underlying collection isn't empty. If so, returns
Unspecified.Optional<T>.Map<U>A utility method to map the value in the
Optionalto another. This removes a lot ofIsSpecifiedchecks inside API code.Take for example #2724, where there was an extra check due to
IsSpecifiedWhen using
Optional<T>.Map, this code gets much easier to read:Just these three methods alone can help clean up a lot of the code base as is:
Beta Was this translation helpful? Give feedback.
All reactions