- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
whither enumerations?
Wed, 2011-01-19, 15:47
hi
at least, just by googling to try to learn about how to do
enumerations in Scala, i see mostly problems, bugs, complaints,
trouble, unrest, general malaise. is there either a great concise
enumerations-in-Scala-that-won't-drive-you-sad document, or plans to
"fix" enumerations in Scala?
thanks.
Wed, 2011-01-19, 17:07
#2
Re: whither enumerations?
On Wed, Jan 19, 2011 at 4:40 PM, Nils Kilden-Pedersen <nilskp@gmail.com> wrote:
On Wed, Jan 19, 2011 at 8:46 AM, Raoul Duke <raould@gmail.com> wrote:hi
at least, just by googling to try to learn about how to do
enumerations in Scala, i see mostly problems, bugs, complaints,
trouble, unrest, general malaise. is there either a great concise
enumerations-in-Scala-that-won't-drive-you-sad document, or plans to
"fix" enumerations in Scala?
I prefer this pattern:
trait Day
object Day { case object Monday extends Day case object Tuesday extends Day case object Wednesday extends Day case object Thursday extends Day case object Friday extends Day case object Saturday extends Day case object Sunday extends Day }
Throw in a "sealed trait Day" and go for insta-win.
The downside is that you really have no "enumeration", there's no way of iterating or doing lookups.
--
Viktor Klang,
Code Connoisseur
Work: Scalable Solutions
Code: github.com/viktorklang
Follow: twitter.com/viktorklang
Read: klangism.tumblr.com
Wed, 2011-01-19, 18:47
#3
Re: whither enumerations?
For me the enum "support" of Scala is one of the few things where the
language is just blatantly inferior to Java. There are many cases
where you can just use case classes / objects as a replacement, but
sometimes you just need those nice extensible enums as known from
Java.
Also, there is a presentation from Paul where he shows that a single
Java enum is translated to something like 10 case classes in Scala,
increasing the code size by roughly a factor of 20.
This is especially sad since I mostly use enums when I need to get
close to the metal for maximum performance.
I still hope that one day Scala's enum support will be better, but who
knows. With the recent surge in special traits a la DelayedInit and
Dynamic, perhaps we will see something along these lines for enums,
too?
For now I'm building my enums by hand which is tedious and error
prone, but I'm reluctant to use Scala's "infamous" Enumeration class.
Regards,
Ruediger
2011/1/19 Raoul Duke :
> hi
>
> at least, just by googling to try to learn about how to do
> enumerations in Scala, i see mostly problems, bugs, complaints,
> trouble, unrest, general malaise. is there either a great concise
> enumerations-in-Scala-that-won't-drive-you-sad document, or plans to
> "fix" enumerations in Scala?
>
> thanks.
>
Wed, 2011-01-19, 19:17
#4
Re: whither enumerations?
On Wed, Jan 19, 2011 at 9:48 AM, √iktor Klang <viktor.klang@gmail.com> wrote:
Duh, yes, forgot that :-)
Throw in a "sealed trait Day" and go for insta-win.
Duh, yes, forgot that :-)
Thu, 2011-01-20, 09:07
#5
Re: whither enumerations?
2011/1/19 Ruediger Keller <ruediger.keller@rk42.de>
Can you show an example? I'm not not sure it helps but there is this switch annotation
http://www.scala-lang.org/api/current/scala/annotation/switch.html
--
Sébastien
This is especially sad since I mostly use enums when I need to get
close to the metal for maximum performance.
Can you show an example? I'm not not sure it helps but there is this switch annotation
http://www.scala-lang.org/api/current/scala/annotation/switch.html
--
Sébastien
Thu, 2011-01-20, 10:07
#6
Re: whither enumerations?
One example:
I sometimes have very large byte arrays, where each byte is an enum
id. This works as long as you have less than 256 enum values and it
saves you 3/4 of the space. This really adds up, when you have arrays
with a size of tens or hundreds of millions, like I do.
Regards,
Ruediger
2011/1/20 Sébastien Bocq :
> 2011/1/19 Ruediger Keller
>>
>> This is especially sad since I mostly use enums when I need to get
>> close to the metal for maximum performance.
>
> Can you show an example? I'm not not sure it helps but there is this switch
> annotation
> http://www.scala-lang.org/api/current/scala/annotation/switch.html
>
> --
> Sébastien
>
Thu, 2011-01-20, 10:17
#7
Re: whither enumerations?
On 20/01/2011 09:03, Sébastien Bocq wrote:
> Can you show an example? I'm not not sure it helps but there is this switch annotation
> http://www.scala-lang.org/api/current/scala/annotation/switch.html
Interesting annotation, it shows that Scala is attentive to performance...
BTW, is anybody in the Scala team aware of the formatting errors in such page?
Particularly the escaped entities like > which should have been displayed as >
Thu, 2011-01-20, 10:27
#8
Re: whither enumerations?
On 19/01/2011 16:48, √iktor Klang wrote:
> Throw in a "sealed trait Day" and go for insta-win.
I saw this pattern coming up from time to time in the mailing list.
A page like http://www.scala-lang.org/node/123 explains what is 'sealed' (as I suspected,
it is close of 'final' in Java, right?) but what does this bring in the Day object? Only
checking exhaustiveness of pattern matching? (which is already nice) Or something else?
Thu, 2011-01-20, 10:37
#9
Re: whither enumerations?
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Write a fold and combinators. Win.
PS: Scala poops all over on Java for enum support.
On 20/01/11 19:02, Ruediger Keller wrote:
> One example:
>
> I sometimes have very large byte arrays, where each byte is an
> enum id. This works as long as you have less than 256 enum values
> and it saves you 3/4 of the space. This really adds up, when you
> have arrays with a size of tens or hundreds of millions, like I
> do.
>
> Regards, Ruediger
>
>
> 2011/1/20 Sébastien Bocq :
>> 2011/1/19 Ruediger Keller
>>>
>>> This is especially sad since I mostly use enums when I need to
>>> get close to the metal for maximum performance.
>>
>> Can you show an example? I'm not not sure it helps but there is
>> this switch annotation
>> http://www.scala-lang.org/api/current/scala/annotation/switch.html
>>
>>
>>
- --
>> Sébastien
>>
Thu, 2011-01-20, 10:57
#10
Re: whither enumerations?
On 20/01/2011 10:07, I wrote:
> checking exhaustiveness of pattern matching? (which is already nice) Or something else?
That's one of the arguments raised by Aaron in the thread
http://stackoverflow.com/questions/1898932/case-classes-vs-enumerations-...
which have a good discussion on this topic.
Thu, 2011-01-20, 11:17
#11
Re: whither enumerations?
Hi Tony,
would you please give me a small example for enums with folds and
combinators in Scala?
I'm always trying to learn, but just giving me the keywords 'fold' and
'combinator' isn't sufficient for me. I assume I have a basic
understanding of folds and combinators, but I don't see how they would
apply to my problem. I'm sure that there are an infinite amount of
problems that can be elegantly solved with folds and combinators, but
I don't see that for my use case.
Perhaps an example would increase my understanding of the
applicability of folds and combinators.
Regards,
Ruediger
2011/1/20 Tony Morris :
>
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Write a fold and combinators. Win.
>
> PS: Scala poops all over on Java for enum support.
>
> On 20/01/11 19:02, Ruediger Keller wrote:
>> One example:
>>
>> I sometimes have very large byte arrays, where each byte is an
>> enum id. This works as long as you have less than 256 enum values
>> and it saves you 3/4 of the space. This really adds up, when you
>> have arrays with a size of tens or hundreds of millions, like I
>> do.
>>
>> Regards, Ruediger
>>
>>
>> 2011/1/20 Sébastien Bocq :
>>> 2011/1/19 Ruediger Keller
>>>>
>>>> This is especially sad since I mostly use enums when I need to
>>>> get close to the metal for maximum performance.
>>>
>>> Can you show an example? I'm not not sure it helps but there is
>>> this switch annotation
>>> http://www.scala-lang.org/api/current/scala/annotation/switch.html
>>>
>>>
>>>
> - --
>>> Sébastien
>>>
>
>
> - --
> Tony Morris
> http://tmorris.net/
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAk03/TUACgkQmnpgrYe6r60SeQCfQlATBEOPupuwieImw0kv1Z6S
> kFgAn1s82WJwYw4G20rwIA/rHrjsWXyn
> =bwql
> -----END PGP SIGNATURE-----
>
>
Thu, 2011-01-20, 11:47
#12
Re: whither enumerations?
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
object Tri {
// Hi Stefan,
// Feel free to forward this to the mailing list if you think it will
help others.
// Consider the following "enum"
type TriCata[X] = X => X => X => X
// This data type is isomorphic (of equivalent form) to an enum with
three nullary constructors. Using traditional algebraic data types, we
sealed trait Tri
case object This extends Tri
case object That extends Tri
case object Other extends Tri
// In the latter case, we also get constructors.
object TriCata {
// Writing those for TriCata is tedious (note the requirement for
type annotation):
def thiss[X]: TriCata[X] = a => _ => _ => a
def that[X]: TriCata[X] = _ => b => _ => b
def other[X]: TriCata[X] = _ => _ => c => c
// ...
// Suppose now it comes to writing combinators:
type TCB = TriCata[Boolean]
def isThis(c: TCB) = c(true)(false)(false)
def isThat(c: TCB) = c(false)(true)(false)
def isOther(c: TCB) = c(false)(false)(true)
// This too is tedious.
// However, what doesn't happen is an explosion for the given number
of constructors.
// In other words, while we have three constructors here, this
doesn't mean we'd have similarly
// tedious and repetitive code. Instead, we'd write combinators to
help us (And our API user) write more combinators.
// An example of a useful combinator is flatten. Now, when you think
of flatten, you might
// think of List[List[A]] => List[A] or perhaps Option[Option[A]] =>
Option[A].
// However, let me generalise this flatten thing to: M[M[A]] (for
some value of M).
// Then let me replace M with Function1[T, _].
// We get: Function1[T, Function1[T, A]] => Function1[T, A]
// In other words: (T => T => A) => T => A
// This function happens to be once-inhabited
// It returns a function that applies a function to a value twice:
def flattenFunction[T, A](f: T => T => A) = (t: T) => f(t)(t)
// This function is now useful to rewrite isThis:
def isThisAgain(c: TCB) = flattenFunction(c(true))(false)
// We don't really save much here because we are dealing with an
algebra with three constructors.
// However, these functions such as flatten because really useful
// as our example becomes less trivial.
// Importantly, notice that the "false" value did not recur
// as it did with the previous implementation.
// Hope this helps.
}
// PS: I have forwarded this to the mailing list per request by
another user.
}
On 20/01/11 20:08, Ruediger Keller wrote:
> Hi Tony,
>
> would you please give me a small example for enums with folds and
> combinators in Scala?
>
> I'm always trying to learn, but just giving me the keywords 'fold'
> and 'combinator' isn't sufficient for me. I assume I have a basic
> understanding of folds and combinators, but I don't see how they
> would apply to my problem. I'm sure that there are an infinite
> amount of problems that can be elegantly solved with folds and
> combinators, but I don't see that for my use case.
>
> Perhaps an example would increase my understanding of the
> applicability of folds and combinators.
>
> Regards, Ruediger
>
>
>
> 2011/1/20 Tony Morris
>>
> Write a fold and combinators. Win.
>
> PS: Scala poops all over on Java for enum support.
>
> On 20/01/11 19:02, Ruediger Keller wrote:
>>>> One example:
>>>>
>>>> I sometimes have very large byte arrays, where each byte is
>>>> an enum id. This works as long as you have less than 256
>>>> enum values and it saves you 3/4 of the space. This really
>>>> adds up, when you have arrays with a size of tens or hundreds
>>>> of millions, like I do.
>>>>
>>>> Regards, Ruediger
>>>>
>>>>
>>>> 2011/1/20 Sébastien Bocq
>>>>> 2011/1/19 Ruediger Keller
>>>>>>
>>>>>> This is especially sad since I mostly use enums when I
>>>>>> need to get close to the metal for maximum performance.
>>>>>
>>>>> Can you show an example? I'm not not sure it helps but
>>>>> there is this switch annotation
>>>>> http://www.scala-lang.org/api/current/scala/annotation/switch.html
>
>>>>>
>>>>>
>>>>
>>>>>
>>>>>
>>
>>
- --
Tony Morris
http://tmorris.net/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAk04DtgACgkQmnpgrYe6r637CQCgi3kJgfW6gZKp++Jn7FhEBmfE
OlMAoLorjRjVCly0PbjM+LaRkgZpI7kA
=gMAz
-----END PGP SIGNATURE-----
Thu, 2011-01-20, 19:27
#13
Re: whither enumerations?
hi,
many thanks to all for all the input, but man i sure wish it wasn't
some roll-your-own-with-chicken-blood approach. i mean, even just the
cross-code incompatibility problem is obviously sad. for something as
"simple" as enums.
is Scala the core language hoping to do something to make Enumeration
be something more acceptable to everybody who actually develops using
Scala?
gracias.
Thu, 2011-01-27, 12:27
#14
Re: whither enumerations?
I once asked this question on StackOverflow:
http://stackoverflow.com/questions/1321745/scala-doesnt-have-enums-what-to-use-instead-of-an-enum
- Jesper
2011/1/20 Raoul Duke <raould@gmail.com>
--
Jesper de Jong
jespdj@gmail.com
http://stackoverflow.com/questions/1321745/scala-doesnt-have-enums-what-to-use-instead-of-an-enum
- Jesper
2011/1/20 Raoul Duke <raould@gmail.com>
hi,
many thanks to all for all the input, but man i sure wish it wasn't
some roll-your-own-with-chicken-blood approach. i mean, even just the
cross-code incompatibility problem is obviously sad. for something as
"simple" as enums.
is Scala the core language hoping to do something to make Enumeration
be something more acceptable to everybody who actually develops using
Scala?
gracias.
--
Jesper de Jong
jespdj@gmail.com
I prefer this pattern:
trait Day
object Day { case object Monday extends Day case object Tuesday extends Day case object Wednesday extends Day case object Thursday extends Day case object Friday extends Day case object Saturday extends Day case object Sunday extends Day }