2

C23 proposal n3003 and n2366 mention a proposed _Either type in passing on the first page and seventh, respectively, and I have not been able to find any other references to it thus far. As far as I can tell, it is neither mentioned in the C23 working draft nor in any of the other C23 proposals.

_Either looks to be proposed as a sum type similar to Rust enums. I imagine that it is a compile-time construct that forces the programmer to check both the left and right type cases anytime it is evaluated. Are there any mentions of _Either that I have missed? Does anyone have any thoughts about what exactly _Either is and how it's implemented?

William Ryman
  • 231
  • 2
  • 9
  • 1
    I found this: https://groups.google.com/a/isocpp.org/g/std-proposals/c/HV-7y5XZmBw/m/_Teb4B64DAAJ – interjay May 16 '23 at 13:26
  • 1
    Neither of these proposals seem to have been voted in with C23. – Lundin May 16 '23 at 13:45
  • 1
    As far as I can tell, n2366 *does not* mention `_Either`. n3003's reference to n2366 seems to be about generic data structures generally, not about `_Either` in particular. – John Bollinger May 16 '23 at 13:46
  • @JohnBollinger n2366 actual _does_ mention `_Either`, albeit briefly. You actually have to CTRL + F "either" in order to find it. I didn't see it at first, either. – William Ryman May 16 '23 at 13:57
  • @Lundin I'm interested in these C proposals because I'm thinking about designing a systems programming language and am interested in "C-style" features. – William Ryman May 16 '23 at 14:05
  • 2
    How odd, @WilliamRyman. I had searched unsuccessfully for `_Either`, but as you say, I found it by searching for "either". – John Bollinger May 16 '23 at 14:07

1 Answers1

3

What Exactly is the Proposed C23 _Either Type?

No such type has been formally proposed for C. It appears to be an idea originating from WG21 (C++) participants, but I don't think any such thing has formally been proposed for C++, either.

It looks like this is the relevant discussion document: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0709r4.pdf. It does not provide an actual specification of _Either, but it uses the symbology _Either(A, B), for types A and B, to represent a type that can contain (or be) either an A or a B, and which somehow indicates which of those any given instance does contain. That might be implemented as a discriminated union, for example, or the document suggests that it might also be left entirely up to the implementation, similar to va_args.

This is related to C23 proposals n3003 and n2366 inasmuch as those touch on generic types and (compatible) type redefinition, which might be relevant to implementing something along the lines of _Either().

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • 3
    Begone!! Foul beast!! IMO C needs no pollution from C++'s benighted and overly-complex "Computer-Scientists-with-too-much-time-on-their-hands" imposition of "cool features because we can" that no one uses. – Andrew Henle May 16 '23 at 15:12
  • 3
    I feel you, @AndrewHenle. For what it's worth, though, the C++ context is a genuine design problem around error and exceptional condition handling, and the main target here is essentially to provide built-in support for `return`ing different, statiically-known types of objects in failure cases than in success cases. C++ exceptions never quite have served acceptably in that general area. We feel it a little in C, too. I admit to some interest in this particular idea, but I'm not prepared to argue for accepting it as a language feature. – John Bollinger May 16 '23 at 15:37
  • 2
    *built-in support for `return`ing different, statically-known types of objects in failure cases* I'd love to be a fly on the wall when some wag asks, "So we're proposing that C++ adopt the Java checked exception model?" in the C++ committee meeting. :-) – Andrew Henle May 18 '23 at 23:18