Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[watchkit] Add nullability to (generated and manual) bindings #14972

Merged
merged 4 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/WatchKit/WKAccessibility.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable

using System;
using System.Runtime.InteropServices;
using Foundation;
Expand Down
20 changes: 11 additions & 9 deletions src/WatchKit/WKInterfaceController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright 2014-2015 Xamarin Inc. All rights reserved.

#nullable enable

#if WATCH

using System;
Expand All @@ -15,31 +17,31 @@ public partial class WKInterfaceController {

public void PushController (string name, string context)
{
using (var ns = context == null ? null : new NSString (context)) {
PushController (name, (NSObject) ns);
using (var ns = context is null ? null : new NSString (context)) {
PushController (name, (NSObject?) ns);
}
}

public void PresentController (string name, string context)
{
using (var ns = context == null ? null : new NSString (context)) {
PresentController (name, (NSObject) ns);
using (var ns = context is null ? null : new NSString (context)) {
PresentController (name, (NSObject?) ns);
}
}

public void PresentController (string [] names, string [] contexts)
{
NSObject[] array = null;
NSObject[]? array = null;
try {
if (contexts != null) {
if (contexts is not null) {
array = new NSObject [contexts.Length];
for (int i = 0; i < array.Length; i++)
array [i] = new NSString (contexts [i]);
}
PresentController (names, array);
}
finally {
if (array != null) {
if (array is not null) {
foreach (var ns in array)
ns.Dispose ();
}
Expand All @@ -56,8 +58,8 @@ static Selector GetUniqueSelector ()
MethodInfo GetMethodInfo (Action action)
{
var del = action as Delegate;
if (del == null)
throw new ArgumentNullException ("action");
if (del is null)
ObjCRuntime.ThrowHelper.ThrowArgumentNullException (nameof (action));
var met = del.Method;
// <quote>The method must be defined on the current interface controller object.</quote>
if (met.DeclaringType != GetType ())
Expand Down
2 changes: 2 additions & 0 deletions src/WatchKit/WKInterfaceDevice.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright 2015 Xamarin Inc. All rights reserved.

#nullable enable

#if WATCH

using System;
Expand Down
2 changes: 2 additions & 0 deletions src/WatchKit/iOS/WKAccessibility.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable

#if __IOS__ && !NET
using System;
using System.ComponentModel;
Expand Down
2 changes: 2 additions & 0 deletions src/WatchKit/iOS/WKAccessibilityImageRegion.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable

#if __IOS__ && !NET
using System;
using System.ComponentModel;
Expand Down
2 changes: 2 additions & 0 deletions src/WatchKit/iOS/WKErrorCode.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable

#if __IOS__ && !NET
using System;
using System.ComponentModel;
Expand Down
2 changes: 2 additions & 0 deletions src/WatchKit/iOS/WKImageAnimatable.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable

#if __IOS__ && !NET
using System;
using System.ComponentModel;
Expand Down
2 changes: 2 additions & 0 deletions src/WatchKit/iOS/WKInterfaceButton.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable

#if __IOS__ && !NET
using System;
using System.ComponentModel;
Expand Down
2 changes: 2 additions & 0 deletions src/WatchKit/iOS/WKInterfaceController.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable

#if __IOS__ && !NET
using System;
using System.ComponentModel;
Expand Down
2 changes: 2 additions & 0 deletions src/WatchKit/iOS/WKInterfaceDate.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable

#if __IOS__ && !NET
using System;
using System.ComponentModel;
Expand Down
2 changes: 2 additions & 0 deletions src/WatchKit/iOS/WKInterfaceDevice.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable

#if __IOS__ && !NET
using System;
using System.Collections.Generic;
Expand Down
2 changes: 2 additions & 0 deletions src/WatchKit/iOS/WKInterfaceGroup.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable

#if __IOS__ && !NET
using System;
using System.ComponentModel;
Expand Down
2 changes: 2 additions & 0 deletions src/WatchKit/iOS/WKInterfaceImage.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable

#if __IOS__ && !NET
using System;
using System.ComponentModel;
Expand Down
2 changes: 2 additions & 0 deletions src/WatchKit/iOS/WKInterfaceLabel.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable

#if __IOS__ && !NET
using System;
using System.ComponentModel;
Expand Down
2 changes: 2 additions & 0 deletions src/WatchKit/iOS/WKInterfaceMap.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable

#if __IOS__ && !NET
using System;
using System.ComponentModel;
Expand Down
2 changes: 2 additions & 0 deletions src/WatchKit/iOS/WKInterfaceMapPinColor.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable

#if __IOS__ && !NET
using System;
using System.ComponentModel;
Expand Down
2 changes: 2 additions & 0 deletions src/WatchKit/iOS/WKInterfaceObject.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable

#if __IOS__ && !NET
using System;
using System.ComponentModel;
Expand Down
2 changes: 2 additions & 0 deletions src/WatchKit/iOS/WKInterfaceSeparator.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable

#if __IOS__ && !NET
using System;
using System.ComponentModel;
Expand Down
2 changes: 2 additions & 0 deletions src/WatchKit/iOS/WKInterfaceSlider.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable

#if __IOS__ && !NET
using System;
using System.ComponentModel;
Expand Down
2 changes: 2 additions & 0 deletions src/WatchKit/iOS/WKInterfaceSwitch.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable

#if __IOS__ && !NET
using System;
using System.ComponentModel;
Expand Down
2 changes: 2 additions & 0 deletions src/WatchKit/iOS/WKInterfaceTable.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable

#if __IOS__ && !NET
using System;
using System.ComponentModel;
Expand Down
2 changes: 2 additions & 0 deletions src/WatchKit/iOS/WKInterfaceTimer.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable

#if __IOS__ && !NET
using System;
using System.ComponentModel;
Expand Down
2 changes: 2 additions & 0 deletions src/WatchKit/iOS/WKMenuItemIcon.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable

#if __IOS__ && !NET
using System;
using System.ComponentModel;
Expand Down
2 changes: 2 additions & 0 deletions src/WatchKit/iOS/WKTextInputMode.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable

#if __IOS__ && !NET
using System;
using System.ComponentModel;
Expand Down
4 changes: 3 additions & 1 deletion src/WatchKit/iOS/WKUserNotificationInterfaceController.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable

#if __IOS__ && !NET
using System;
using System.ComponentModel;
Expand Down Expand Up @@ -45,7 +47,7 @@ public virtual void DidReceiveRemoteNotification (NSDictionary remoteNotificatio
throw new PlatformNotSupportedException (Constants.WatchKitRemoved);
}

public virtual void DismissController ()
public new virtual void DismissController ()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is surprising. What's the story here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chamons Good question, the compiler told me that I should and I kind of understood so I did, but because you asked, I read more on it and understand it a little better now!
The WKUserNotificationInterfaceController class inherits from WKInterfaceController and both classes contain a method definition for DismissController. The WKUserNotificationInterfaceController version above does not contain an 'override' keyword so implicitly, the compiler is hiding the base class version of DismissController inside WKInterfaceController. The compiler gives us a warning saying that it is already doing this, but we should mark it with the new keyword to be explicit. In this case however, both classes have the same definition for DismissController so I think adding a new or an override would give us the same results and perhaps we could even remove this method altogether?

That last part I am not 100% sure though and would like to know if others know if there would be any effects if we remove this method. If we have BaseClass, Derived1Class : BaseClass, and Derived2Class : Derived1Class and BaseClass and Derived1Class both contain different method implementations for Method1 (), if we call base.Method1 () from Derived2Class, will it call BaseClass.Method1 () or Derived1Class.Method1 ()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this would be good for me to bring to office hours ^

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait testing it out and we can't add a virtual and override on a method so that was actually not an option in my top answer :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't remove the method, because that's a breaking change.

In this case adding the new keyword is the correct thing to do, because it preserves the old behavior (the only change with the new keyword is that it silences the warning).

Changing to override would change the method to override the base method, which while it would probably be the correct thing to do from a design stand point, the fact is that this is deprecated API that doesn't even exist in the OS anymore, so our only concern is to not break backwards compatibility.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long thread.. so I tested it out and found out that in my new scenario, the Derived2Class's base will refer to Derived1Class!

{
throw new PlatformNotSupportedException (Constants.WatchKitRemoved);
}
Expand Down
2 changes: 2 additions & 0 deletions src/WatchKit/iOS/WKUserNotificationInterfaceType.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable

#if __IOS__ && !NET
using System;
using System.ComponentModel;
Expand Down