dart-windows / win32

Build Win32 apps with Dart!
https://win32.pub
BSD 3-Clause "New" or "Revised" License
735 stars 118 forks source link

Mark optional parameters in Win32 functions and COM methods as nullable #822

Open halildurmus opened 4 months ago

halildurmus commented 4 months ago

In certain Win32 functions and COM methods, some parameters are designated as optional, indicating that a 0 (NULL) or nullptr can be passed as an argument, depending on the parameter type.

To enhance the developer experience, these optional parameters can be marked as nullable by the generator. This allows 0 or nullptr to be passed implicitly if the caller passes null.

Consider the CreateWindowEx function as an example, currently projected as:

int CreateWindowEx(
        int dwExStyle,
        Pointer<Utf16> lpClassName,
        Pointer<Utf16> lpWindowName,
        int dwStyle,
        int X,
        int Y,
        int nWidth,
        int nHeight,
        int hWndParent,
        int hMenu,
        int hInstance,
        Pointer lpParam) =>
    _CreateWindowEx(dwExStyle, lpClassName, lpWindowName, dwStyle, X, Y, nWidth,
        nHeight, hWndParent, hMenu, hInstance, lpParam);

final _CreateWindowEx = _user32.lookupFunction<
    IntPtr Function(
        Uint32 dwExStyle,
        Pointer<Utf16> lpClassName,
        Pointer<Utf16> lpWindowName,
        Uint32 dwStyle,
        Int32 X,
        Int32 Y,
        Int32 nWidth,
        Int32 nHeight,
        IntPtr hWndParent,
        IntPtr hMenu,
        IntPtr hInstance,
        Pointer lpParam),
    int Function(
        int dwExStyle,
        Pointer<Utf16> lpClassName,
        Pointer<Utf16> lpWindowName,
        int dwStyle,
        int X,
        int Y,
        int nWidth,
        int nHeight,
        int hWndParent,
        int hMenu,
        int hInstance,
        Pointer lpParam)>('CreateWindowExW');

Here's the proposed projection for this function, marking the optional parameters as nullable and implicitly passing 0 or nullptr if the caller passes null:

int CreateWindowEx(
        int dwExStyle,
        Pointer<Utf16>? lpClassName,
        Pointer<Utf16>? lpWindowName,
        int dwStyle,
        int x,
        int y,
        int nWidth,
        int nHeight,
        int? hWndParent,
        int? hMenu,
        int? hInstance,
        Pointer? lpParam) =>
    _CreateWindowEx(
        dwExStyle,
        lpClassName ?? nullptr,
        lpWindowName ?? nullptr,
        dwStyle,
        x,
        y,
        nWidth,
        nHeight,
        hWndParent ?? 0,
        hMenu ?? 0,
        hInstance ?? 0,
        lpParam ?? nullptr);

final _CreateWindowEx = _user32.lookupFunction<
    HWND Function(
        Uint32 dwExStyle,
        Pointer<Utf16> lpClassName,
        Pointer<Utf16> lpWindowName,
        Uint32 dwStyle,
        Int32 x,
        Int32 y,
        Int32 nWidth,
        Int32 nHeight,
        HWND hWndParent,
        HMENU hMenu,
        HINSTANCE hInstance,
        Pointer lpParam),
    int Function(
        int dwExStyle,
        Pointer<Utf16> lpClassName,
        Pointer<Utf16> lpWindowName,
        int dwStyle,
        int x,
        int y,
        int nWidth,
        int nHeight,
        int hWndParent,
        int hMenu,
        int hInstance,
        Pointer lpParam)>('CreateWindowExW');