Skip to content

proposal: cmp: Add Cond[T any](cond bool, ifval, elseval T) T #66062

@earthboundkid

Description

@earthboundkid

Proposal Details

Recently, #64825 was marked as a likely declined proposal. This is an alternate proposal with a similar goal: to make writing simple, commonly used bool conversions easier. Here is the proposed addition:

func Cond[T any](cond bool, ifval, elseval T) T {
   if cond {
     return ifval
  }
  return elseval
}

I have been using a helper like this in my code since Go 1.18 and I find it convenient to have. The bool conversions mentioned in #64825 would be cmp.Cond(b, 1, 0) with this alternative proposal.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions