views:

276

answers:

2

Basically, I have an array like this:

val base_length = Array(
    0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56,
    64, 80, 96, 112, 128, 160, 192, 224, 0
  );

And when scala sees it, it wants to do this:

base_length: Array[Int] = Array(...)

But I would prefer for it to do this:

base_length: Array[Byte] = Array(...)

I tried:

val base_length = Array[Byte](...)

But scala says:

<console>:4: error: type arguments [Byte] do not conform to method apply's type
parameter bounds [A <: AnyRef]
       val base_length = Array[Byte](1,2,3,4,5)

This seems to me to basically be telling me that the Array constructor wants to figure out what the type of the array is from the arguments. Normally that's awesome, but in this instance I have good reasons for wanting the array elements to be Bytes.

I have looked around for guidance on this, but I don't seem to be able to find anything. Any help would be great!

+4  A: 

It should be:

C:\prog\>scala
Welcome to Scala version 2.7.5.final (Java HotSpot(TM) Client VM, Java 1.6.0_16).
Type in expressions to have them evaluated.
Type :help for more information.

scala> val gu: Array[Byte] = Array(18, 19, 20)
gu: Array[Byte] = Array(18, 19, 20)

This is not immutable. A Seq would be a step in that direction even if it is only a trait (as Christopher mentions in the comments) adding finite sequences of elements. A Scala List would be immutable.

VonC
In fact, Seq is a trait. An Array has the Seq trait, but this doesn't really have anything to do with being immutable. Instead, it has to do with being ordered.
Christopher
This appears to solve my problem, thanks!
Christopher
A list would be immutable, but has very poor random access qualities. An array is appropriate for this situation because it is a lookup table.
Christopher
+1  A: 

Works in Scala 2.8.0:

Welcome to Scala version 2.8.0.r18502-b20090818020152 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_15).
Type in expressions to have them evaluated.
Type :help for more information.

scala> Array[Byte](0, 1, 2)
res0: Array[Byte] = Array(0, 1, 2)
Walter Chang