How can I force an array type upon initialization in Scala?

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 this, he wants to do it:

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

But I would prefer to do this for 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 type parameter bounds [A <: AnyRef] val base_length = Array[Byte](1,2,3,4,5) 

It seems to me that basically I'm saying that the Array constructor wants to figure out what type of array is the argument. This is usually awesome, but in this case I have good reason for the elements of the array to be Byte s.

I was looking for a guide for this, but it looks like I can't find anything. Any help would be great!

+6
arrays scala templates
source share
2 answers

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. Seq will be a step in that direction, even if it is only a sign (as Christopher mentions in the comments), adding finite sequences of elements. The Scala list would be immutable.

+5
source share

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) 
+1
source share

All Articles