Hi,
While investigating crash reports that appear to originate from WatchUi.getCurrentView(), I started working on a safe wrapper that explicitly validates all returned types.
What I find confusing is that I seem to be unable to check whether the returned delegate is an instance of BehaviorDelegate. The compiler reports an error stating that the delegate can never be an instance of BehaviorDelegate.
According to the documentation for WatchUi.InputDelegates, this should be possible. However, the compiler seems to disagree.
I’d appreciate your thoughts on this behavior. Is this a compiler bug, or am I doing something wrong?
Below is my code, followed by the compiler error message.
46 public function getCurrentViewSafe() as [ WatchUi.View or Null, WatchUi.InputDelegates or Null ] {
47
48 var cwArray = WatchUi.getCurrentView();
49 if( ! (cwArray instanceof Array ) ) {
50 throw new GeneralException( "cwArray not an Array" );
51 }
52 if( cwArray.size() != 2 ) {
53 throw new GeneralException( "cwArray.size = " + cwArray.size() );
54 }
55
56 var view = cwArray[0];
57 if( view != null && ! ( view instanceof WatchUi.View ) ) {
58 throw new GeneralException( "cwArray[0] not a View" );
59 }
60
61 var delegate = cwArray[1];
62 if( delegate != null &&
63 ! ( delegate instanceof WatchUi.InputDelegate
64 || delegate instanceof WatchUi.BehaviorDelegate
65 || delegate instanceof WatchUi.ConfirmationDelegate
66 || delegate instanceof WatchUi.MenuInputDelegate
67 || delegate instanceof WatchUi.NumberPickerDelegate
68 || delegate instanceof WatchUi.PickerDelegate
69 || delegate instanceof WatchUi.TextPickerDelegate
70 || delegate instanceof WatchUi.WatchFaceDelegate
71 || delegate instanceof WatchUi.Menu2InputDelegate
72 || delegate instanceof WatchUi.ViewLoopDelegate )
73 ) {
74 throw new GeneralException( "cwArray[1] not an input delegate" );
75 }
76
77 return cwArray;
78 }
WARNING: epix2pro47mm: D:\GitHub\ohg\source\user-interface\view-handling\ViewHandler.mc:50,12: Statement is not reachable.
WARNING: epix2pro47mm: D:\GitHub\ohg\source\user-interface\view-handling\ViewHandler.mc:53,12: Statement is not reachable.
WARNING: epix2pro47mm: D:\GitHub\ohg\source\user-interface\view-handling\ViewHandler.mc:58,12: Statement is not reachable.
WARNING: epix2pro47mm: D:\GitHub\ohg\source\user-interface\view-handling\ViewHandler.mc:62,8: '$.Toybox.WatchUi.NumberPickerDelegate' is deprecated.
WARNING: epix2pro47mm: D:\GitHub\ohg\source\user-interface\view-handling\ViewHandler.mc:74,12: Statement is not reachable.
ERROR: epix2pro47mm: D:\GitHub\ohg\source\user-interface\view-handling\ViewHandler.mc:62,8: Type 'PolyType<$.Toybox.WatchUi.ConfirmationDelegate or $.Toybox.WatchUi.Menu2InputDelegate or $.Toybox.WatchUi.MenuInputDelegate or $.Toybox.WatchUi.NumberPickerDelegate or $.Toybox.WatchUi.PickerDelegate or $.Toybox.WatchUi.TextPickerDelegate or $.Toybox.WatchUi.ViewLoopDelegate or $.Toybox.WatchUi.WatchFaceDelegate>' is not an instance of '$.Toybox.WatchUi.BehaviorDelegate'.
Regards, Robert

