From 4adf8611131a5d86b15f017a42a0366837d14528 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 13 Apr 2022 21:07:43 -0400 Subject: enable nullable annotations in the rest of SMAPI core (#837) --- src/SMAPI/Utilities/KeybindList.cs | 2 +- src/SMAPI/Utilities/PerScreen.cs | 23 ++++++++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) (limited to 'src/SMAPI/Utilities') diff --git a/src/SMAPI/Utilities/KeybindList.cs b/src/SMAPI/Utilities/KeybindList.cs index 18eeb9fd..aa12a37a 100644 --- a/src/SMAPI/Utilities/KeybindList.cs +++ b/src/SMAPI/Utilities/KeybindList.cs @@ -50,7 +50,7 @@ namespace StardewModdingAPI.Utilities /// The keybind string. See remarks on for format details. /// The parsed keybind list, if valid. /// The errors that occurred while parsing the input, if any. - public static bool TryParse(string input, [NotNullWhen(true)] out KeybindList? parsed, out string[] errors) + public static bool TryParse(string? input, [NotNullWhen(true)] out KeybindList? parsed, out string[] errors) { // empty input if (string.IsNullOrWhiteSpace(input)) diff --git a/src/SMAPI/Utilities/PerScreen.cs b/src/SMAPI/Utilities/PerScreen.cs index afe3ba91..799ff63b 100644 --- a/src/SMAPI/Utilities/PerScreen.cs +++ b/src/SMAPI/Utilities/PerScreen.cs @@ -1,8 +1,7 @@ -#nullable disable - using System; using System.Collections.Generic; using System.Linq; +using StardewModdingAPI.Framework; namespace StardewModdingAPI.Utilities { @@ -39,14 +38,28 @@ namespace StardewModdingAPI.Utilities ** Public methods *********/ /// Construct an instance. + /// Limitation with nullable reference types: when the underlying type is nullable, this sets the default value to null regardless of whether you marked the type parameter nullable. To avoid that, set the default value with the 'createNewState' argument instead. public PerScreen() - : this(null) { } + : this(null!) { } /// Construct an instance. /// Create the initial state for a screen. public PerScreen(Func createNewState) { - this.CreateNewState = createNewState ?? (() => default); + // ReSharper disable once ConditionIsAlwaysTrueOrFalse -- required for backwards compatibility + if (createNewState is null) + { + SCore.DeprecationManager.Warn( + SCore.DeprecationManager.GetSourceNameFromStack(), + $"calling the {nameof(PerScreen)} constructor with null", + "3.14.0", + DeprecationLevel.Notice + ); + + createNewState = (() => default!); + } + + this.CreateNewState = createNewState; } /// Get all active values by screen ID. This doesn't initialize the value for a screen ID if it's not created yet. @@ -61,7 +74,7 @@ namespace StardewModdingAPI.Utilities public T GetValueForScreen(int screenId) { this.RemoveDeadScreens(); - return this.States.TryGetValue(screenId, out T state) + return this.States.TryGetValue(screenId, out T? state) ? state : this.States[screenId] = this.CreateNewState(); } -- cgit