Skip to content

Unhelpful compiler error for @Flag var _: Bool? without a default value #835

@ktoso

Description

@ktoso

The following should be supported but isn't:

    @Flag(help: "...")
    var writeEmptyFiles: Bool?

The error is

Referencing initializer 'init(name:help:)' on 'Flag' requires the types 'Bool?' and 'Int' be equivalent

It turns out the inversion parameter is required, so the following works:

    @Flag(inversion: .prefixedEnableDisable, help: "")
    var writeEmptyFiles: Bool?

this seems somewhat accidental? Are we just missing an initializer with defaulted inversion?

ArgumentParser version: 0.1.0 or the main branch, for example.
Swift version: swift-driver version: 1.139 Apple Swift version 6.2 (swiftlang-6.3.0.101.15 clang-2100.0.101.15); arm64-apple-macosx26.0

Checklist

Steps to Reproduce

The snippets above are enough to repro

Expected behavior

We should support just @Flag() on optional boolean fields

Actual behavior

fails to compile

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions