« Return to Thread: [scala] fixed array type

Re: [scala] fixed array type

by Viktor Klang :: Rate this Message:

Reply to Author | View in Thread

But it's fairly easy to wrap the existing array types to create something that might do it for you.


scala> trait FixedArray[T] {
     |     val length : Int;
     |     private[this] val array = new Array[T](length)
     |     def update(a : Int, c : T) = array(a) = c;
     |     def apply(a : Int) = array(a)
     | }
defined trait FixedArray

scala> case class CharArray140 extends FixedArray[Char] { val length = 140 }
defined class CharArray140

scala> CharArray140()
res16: CharArray140 = CharArray140()

scala> res16(0) = 'b'

scala> res16(0)
res18: Char = b


The length of the array is compile-time bounded, but it won't be stack-allocated.
I'm still unsure wether you asked for language builtin support or if the sketchy suggestion above will suffice for your purposes.



On Wed, Jun 17, 2009 at 12:24 AM, Balthazar Crowley <balthazar.crowley@...> wrote:
Jorge,

Thanks for the response. Unfortunately, that expression does not denote a type. And the type of the thing it does denote is not what is required.

Regards,

Balthazar

scala> new Array[Char](140)
res20: Array[Char] = Array(, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...
scala> 

On Tue, Jun 16, 2009 at 2:53 PM, Jorge Ortiz <jorge.ortiz@...> wrote:
new Array[Char](140) ?

--j


On Tue, Jun 16, 2009 at 2:39 PM, Balthazar Crowley <balthazar.crowley@...> wrote:
Viktor,

Since this is only for pedagogical purposes, either will do. The example might be familiar to many: Char arrays limited to 140 elements.

Regards,

Balthazar


On Tue, Jun 16, 2009 at 12:28 PM, Viktor Klang <viktor.klang@...> wrote:


On Tue, Jun 16, 2009 at 9:24 PM, Balthazar Crowley <balthazar.crowley@...> wrote:
All,

Does Scala have a fixed array type?

As opposed to broken ones?

Jokes aside, are you talking about stack-allocated fixed arrays or only that they are fixed as in compile-time size bounded?

 


Regards,

Balthazar

--
Balthazar Crowley
Resident Magician
DSLver.com



--
Viktor Klang
Scala Loudmouth



--
Balthazar Crowley
Resident Magician
DSLver.com




--
Balthazar Crowley
Resident Magician
DSLver.com



--
Viktor Klang
Scala Loudmouth

 « Return to Thread: [scala] fixed array type