> a decent enum is almost within reach
holy cow, like we are living in the future! this is exciting.
A withName returning Option[Value] instead of Value would be nice as well.
Do I understand correctly that you suggest to write an enumerated type
as follows:
Wouldn't standard Scala syntax work, i.e.
final val B1, B2, B3 = new Bippy
All I can say is that even in Pascal, which is over 40 years old it looks nicer:
type enum = (B1, B2);
See various solutions to enumerated types here:
http://en.wikipedia.org/wiki/Enumerated_type
Scala is about to come out as the worst of these solutions.
Klaus
> Wouldn't standard Scala syntax work, i.e.
>
> final val B1, B2, B3 = new Bippy
>
> ?
>
> --Rex
>
>
> On Tue, Jun 5, 2012 at 9:34 AM, Klaus Havelund <have...@gmail.com> wrote:
>>
>> Do I understand correctly that you suggest to write an enumerated type
>> as follows:
>>
>> object enum {
>> sealed class Bippy private[enum] ()
>>
>> final val B1 = new Bippy
>> final val B2 = new Bippy
>> final val B3 = new Bippy
>> }
>>
>> If have to admit that I find this notation slightly verbose.
>
>
Scala is about to come out as the worst of these solutions.
Requiring lazy could be a little rough though. I imagine it could be the source of much confusion and feel a little ad-hoc.
Would it be possible to get around it with the delayed initialization used to implement App?
BR
John
You could also integrate a type-level natural number (shapeless) and provide a bijection lens to the enumeration. Or just do what java does and use Intyuk.
On question number 1: I think it makes a lot of sense to emit bytecode equivalent to Java enums. Currently, they are substantially different (and incompatible) and it is just a PITA, without any good reason.
On question number 2: As much as I hate special cases, maybe this is one of the cases where it needs to happen. What about some “enum” keyword which does comparable magic as “case”. For instance, “enum” could generate the methods which are also generated by javac and probably add some apply/unapply methods for convenience/pattern matching.
In the end, I think Scala should use Java enums, either by emitting the corresponding bytecode or by telling that scala has no enums and people should use javac to define their enums. The current situation is that we have two different approaches, both incompatible with each other and with their own benefits and drawbacks, and as soon as a developer wants to enable use from Java, he has to rewrite them as Java enums.
Personally, I tend to use 100% Java enums and It would be great being able to define them without having to go back to javac.
TL;DR:
The current approaches are not great, would it be possible to map Scala enums to Java enums in the compiler and be done with it, once and for all?
Thanks and bye,
Simon
sincerely.
I personnally hate the idea of Scala being so dependable on Java. It makes me feel like I'm using half a language, and when promoting Scala, if someone asks me a question where I can only answer "it uses Java for that", I feel almost ashamed.
-1 to simply using Java enums, despite the fact that they work quite well...
I personnally hate the idea of Scala being so dependable on Java. It makes me feel like I'm using half a language, and when promoting Scala, if someone asks me a question where I can only answer "it uses Java for that", I feel almost ashamed.
Sorry, I don't follow ... do you disagree with using Java-the-language for defining enums or that Scala should emit bytecode which looks like “real” Java enums?
-1 to simply using Java enums, despite the fact that they work quite well...
Well, Java enums are the only option if you want to be efficient, interoperable and bug-free. I would love if there was a way in Scala to write them. Until that happens, I just use javac.
Do Java enums have exhaustiveness checks? I think not. So are we collectively barking up the wrong tree here?
Taking a step back, can we identify what's wrong with Enumeration today (never mind it's bug-ridden past). I can identify two problem areas:(1) Automatic name detection is fickle because it relies on coding patterns that can be circumvented.(2) All enums erase to the same type so you cannot overload methods that take different enum types.Is there anything else?
Thanks and bye,
Simon
Do Java enums have exhaustiveness checks? I think not. So are we collectively barking up the wrong tree here?
Just checked. You are absolutely right, it doesn't. I was totally sure javac would bark at me
Is there anything else?
> << Total example fail >>
// Only ONE CLASSFILE no matter how many Bippies!// Contrast with "object B1 extends Bippy", where we// end up with two classes (B1 and B1$) per node.object enum {// Oh, we can't make Bippy abstract else every instance has to be a subclass// A private constructor will have to suffice to "seal" it.sealed class Bippy private[enum] ()final val B1 = new Bippy // infers B1.typefinal val B2 = new Bippy // infers B2.typefinal val B3 = new Bippy // infers B3.type
// Now what we need is for scala to notice that Bippy is sealed// with a constructor which can only be invoked from this file, and// thus deduce that its *instances* are sealed - and we do not// need to cover "Bippy", only B1.type, B2.type, B3.type.def f(x: Bippy) = x match {case B1 => 1 // B1.type is coveredcase B2 => 2 // B2.type is covered}// hypothetically speakingwarning: match may not be exhaustive.It would fail on the following input: B3def f(x: Bippy) = x match {^ }}
Do Java enums have exhaustiveness checks? I think not. So are we collectively barking up the wrong tree here?
.
.
Is there anything else?