Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Represent equivalent enums the same as bool, make bool an enum #330

Closed
wants to merge 1 commit into from

Conversation

glaebhoerl
Copy link
Contributor

@reem
Copy link

reem commented Sep 28, 2014

If we're doing this, which I am in favor of, we should probably changed the type to Bool and the variants to True and False, signaling that it is not so special.

Precedence: Haskell

@blaenk
Copy link
Contributor

blaenk commented Sep 28, 2014

Yeah, making them True and False would be more consistent.

@glaebhoerl
Copy link
Contributor Author

But it would be inconsistent with the C family of languages whose practitioners we are trying to look familiar to. And in any case the two things, naming and implementation, are almost completely orthogonal. I would rather have the capitalization debate separately.

Represent all `enum`s which have two variants, both of which have either no data
or zero-sized data, the same way as `bool` is currently represented. Guarantee
that this will be so. (This includes any user-defined `enum` with two nullary
variants, `Result<(), ()>`, and so on.)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should force this - we currently reserve the right to change all our default data representations and that is nice flexibility to have. You can request this using the c-repr annotation, and of course you could put that on the bool enum, which fulfils your use case, I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to address this under "Arguments against". Do you have a counter-counterargument? I know we do currently reserve this right, but I don't see how the flexibility is at all useful in this case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are certainly things we might want to do - e..g, represent an array of enums as a bit field or something. I think just because we can't think of something now doesn't mean we won't think of something in the future. In particular in this case, because even if we take the rest of this RFC I see essentially no downside in having to annotate bool-like enums with C-repr

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

represent an array of enums as a bit field or something

This specifically would break interior references.

But in general, as noted by another comment, any representation change you'd want to do for bools, you would also want to do for the equivalent types. They only differ, after all, in naming. If you like, this is a specific case of the more general property that representations of types which differ only in their names and the names of their fields/variants should always be the same. (Because there is absolutely no reason the compiler would ever want to choose different representations based on just names.)

@bill-myers
Copy link

This syntax might better achieve the desired goals:

enum Answer : bool
{
  Yes = true,
  No = false
}

@ftxqxd
Copy link
Contributor

ftxqxd commented Sep 29, 2014

@bill-myers The current way of specifying what type to represent an enum with is using the #[repr] attribute:

#[repr(u8)]
enum Foo {
    Bar = 1,
    Baz = 2,
}

Simply allowing #[repr(bool)] would be fine.

@liigo
Copy link
Contributor

liigo commented Sep 29, 2014

bool true false are core language type/values. If defines them in
standard library, people can't use them without core/std crate.

@Ericson2314
Copy link
Contributor

Again, something I thought of proposing once but never got around to. Thank you, @glaebhoerl!

@liigo they would go in Core, which is really minimal and should be usable in all projects.

I agree the representation shouldn't be forced, but I think that strengthens the case for this. Any optimizations that could be done with bools (bitvectors, bitfields, control-flow vs data-flow tricks, conditional mov, etc) ought to be available to all isomorphic types.

Most of those optimizations should be handled by LLVM anyways, so I'd hope this might actually not be too complex.

I'd like to also make all if-relating things macros around pattern matching so we don't need the lang_item, but I recognize that might scare some people. :)

@Ericson2314
Copy link
Contributor

Also +1 for upper case identifiers. I'd like to do upper case names for the integer types too, C/C++/Java be damned, but if this becomes library defined then upper casing it should be given special priority.

@aturon
Copy link
Member

aturon commented Sep 30, 2014

@glaebhoerl At a first glance, it seems like this change could be done post-1.0, backwards-compatibly, but I could easily be wrong. Have you given that any thought?

@glaebhoerl
Copy link
Contributor Author

I assumed it couldn't be, because keywords are "always in scope" whereas prelude entities aren't necessarily. In particular #[no_implicit_prelude] modules would break.

@aturon
Copy link
Member

aturon commented Sep 30, 2014

I assumed it couldn't be, because keywords are "always in scope" whereas prelude entities aren't necessarily. In particular #[no_implicit_prelude] modules would break.

True, although I suspect this could be worked around if need be. But it also seems like the aspect of the proposal involving keywords is separable from the part dealing with the enum representation, and I think the latter could be introduced without any troubles.

(FWIW, I'm in principle in favor of most of the proposal, but we only have so much time before 1.0, so I'm trying to see what the tradeoffs are if we postpone consideration.)

@glaebhoerl
Copy link
Contributor Author

Concerns about representation came up last time this was discussed (can't remember where that was), which is why I took these together.

Remove `true` and `false` as keywords.


# Arguments for
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another argument for is improved documentation, since things can be attached to bool directly & it would get its own doc page in the normal way.

@aturon aturon mentioned this pull request Oct 2, 2014
@aturon
Copy link
Member

aturon commented Oct 2, 2014

We discussed this RFC at the triage meeting today, and decided:

  1. That collectively, we do not see enough advantage to removing the true and false keywords to warrant the pain.
  2. That we are interested in making bool an enum in the long run, but without the keyword removal this can be done later, backwards-compatibly.

Therefore, we are postponing (the enum part of) this RFC. I've created a tracking issue for it.

@aturon aturon closed this Oct 2, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants