Skip to content

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

Duplicate of#36303
@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.

Activity

added this to the Proposal milestone on Mar 1, 2024
seh

seh commented on Mar 1, 2024

@seh
Contributor

This is analogous to a ternary conditional operator, but it evaluates its consequent and alternative values eagerly, which makes it less useful. However, I understand that we're not likely to see such an operator in Go any time soon.

Merovius

Merovius commented on Mar 1, 2024

@Merovius
Contributor

I'll note that it should be

func Cond[T any, B ~bool](cond B, ifval, elseval T) T
x0wllaar

x0wllaar commented on Mar 1, 2024

@x0wllaar

I mean, you can make it lazy if you want: https://go.dev/play/p/D9BDFlGF-Uf

earthboundkid

earthboundkid commented on Mar 1, 2024

@earthboundkid
ContributorAuthor

I'll note that it should be

func Cond[T any, B ~bool](cond B, ifval, elseval T) T

I don't think it's worthwhile to make it generic over bool types. If it were builtin, sure.

Of course, if it were builtin, it should be lazy.

moved this to Incoming in Proposalson Mar 1, 2024
Merovius

Merovius commented on Mar 1, 2024

@Merovius
Contributor

I disagree. bool is a declared type and it would be pretty frustrating, having to convert it if you happen to use a user-declared boolean type (this is different for slices, for example, where type MySlice []T is assignable to []T). Not that declaring boolean types is incredibly common, but it's not unheard of. And the cost seems close to zero. For a standard library function, it seems like a no-brainer to me, that we'd want to make it as general as we can.

Jorropo

Jorropo commented on Mar 2, 2024

@Jorropo
Member

I mean, you can make it lazy if you want: https://go.dev/play/p/D9BDFlGF-Uf

That is impressive extends to go through to not write a simpler if check.

zigo101

zigo101 commented on Mar 2, 2024

@zigo101

#36303 proposal: new builtin to pick from one of two values

#37739 proposal: Go 2: lazy values

Zig's solution:

_ = if (cond) ifval else elseval;
Jorropo

Jorropo commented on Mar 2, 2024

@Jorropo
Member

Python's solution:

_ = ifval if cond else elseval

Skips () but is not intuitive (it's forced by parser limitations and falls out of the requirements but it's still weird).

Jorropo

Jorropo commented on Mar 2, 2024

@Jorropo
Member

Of course, if it were builtin, it should be lazy.

👎 this would be weird magic, currently builtins only cheat about the type system (generics before we had generics and ~string~[]byte signature overload) but not control flow. The value of adding control flow cheats so you can save a couple of ⏎ presses is low to me.

earthboundkid

earthboundkid commented on Mar 2, 2024

@earthboundkid
ContributorAuthor

I had not seen #36303 before. Looks like the builtin version of this has been rejected. I still think the cmp.Cond version is marginally convenient and fits with the general vibe of package cmp.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @seh@earthboundkid@Merovius@gopherbot@x0wllaar

        Issue actions

          proposal: cmp: Add Cond[T any](cond bool, ifval, elseval T) T · Issue #66062 · golang/go