[scala] fixed array type

View: New views
14 Messages — Rating Filter:   Alert me  

[scala] fixed array type

by Balthazar Crowley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

All,

Does Scala have a fixed array type?

Regards,

Balthazar

--
Balthazar Crowley
Resident Magician
DSLver.com

Re: [scala] fixed array type

by Viktor Klang :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



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

Re: [scala] fixed array type

by Balthazar Crowley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

Re: [scala] fixed array type

by Jorge Ortiz-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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


Re: [scala] fixed array type

by Balthazar Crowley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

Re: [scala] fixed array type

by Paul Phillips-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, Jun 16, 2009 at 03:24:10PM -0700, Balthazar Crowley wrote:
> 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.

Oh, it sounds like you want a dependent type.

That is, the type "Array[Char] of length 140." Right?

  http://en.wikipedia.org/wiki/Dependent_type

You can't have that in scala or any remotely mainstream language, but it
is one of my fonder hopes for the future.

--
Paul Phillips      | We act as though comfort and luxury were the chief
Vivid              | requirements of life, when all that we need to make us
Empiricist         | really happy is something to be enthusiastic about.
i pull his palp!   |     -- Charles Kingsley

Re: [scala] fixed array type

by Balthazar Crowley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Paul,

You raise an interesting tangential question. Are languages like Agda not even remotely mainstream? 

Regards,

Balthazar

On Tue, Jun 16, 2009 at 3:32 PM, Paul Phillips <paulp@...> wrote:
On Tue, Jun 16, 2009 at 03:24:10PM -0700, Balthazar Crowley wrote:
> 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.

Oh, it sounds like you want a dependent type.

That is, the type "Array[Char] of length 140." Right?

 http://en.wikipedia.org/wiki/Dependent_type

You can't have that in scala or any remotely mainstream language, but it
is one of my fonder hopes for the future.

--
Paul Phillips      | We act as though comfort and luxury were the chief
Vivid              | requirements of life, when all that we need to make us
Empiricist         | really happy is something to be enthusiastic about.
i pull his palp!   |     -- Charles Kingsley



--
Balthazar Crowley
Resident Magician
DSLver.com

Re: [scala] fixed array type

by Vlad Patryshev :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Yet another side of lambda cube...

2009/6/16 Paul Phillips <paulp@...>
On Tue, Jun 16, 2009 at 03:24:10PM -0700, Balthazar Crowley wrote:
> 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.

Oh, it sounds like you want a dependent type.

That is, the type "Array[Char] of length 140." Right?

 http://en.wikipedia.org/wiki/Dependent_type

You can't have that in scala or any remotely mainstream language, but it
is one of my fonder hopes for the future.

--
Paul Phillips      | We act as though comfort and luxury were the chief
Vivid              | requirements of life, when all that we need to make us
Empiricist         | really happy is something to be enthusiastic about.
i pull his palp!   |     -- Charles Kingsley



--
Thanks,
-Vlad

Re: [scala] fixed array type

by Arrgh :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 16-Jun-09, at 3:32 PM, Paul Phillips wrote:
On Tue, Jun 16, 2009 at 03:24:10PM -0700, Balthazar Crowley wrote:
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.

Oh, it sounds like you want a dependent type.

That is, the type "Array[Char] of length 140." Right?


You can't have that in scala or any remotely mainstream language, but it 
is one of my fonder hopes for the future.

It's not anything like mainstream, but X10 claims to have a form of dependent types and, as of v1.7, has a lot of syntactic (and other) similarities to Scala:

From http://dist.codehaus.org/x10/documentation/languagespec/x10-173.pdf:

Dependent types Classes and interfaces may declare properties: immutable
object members bound at object construction. Types may be defined by
constraining a class or interface’s properties. Value properties enable the
definition of dependent types.

For example, the following code declares a class for a two-dimensional
Point class with an add method.

class Point(x: Int, y: Int) {
  def add(p: Point): Point { 
    return new Point(x+p.x,y+p.y); 
  }
}

The class has integer value properties x and y. The add method creates
and returns a new point by element-wise addition. The dependent type
Point{x==0} is the type of all points with x set to 0; that is, all points
along the y-axis.

And... 

4.5 Constrained types
Given a type T, a constrained type T{e} may be constructed by constraining
its properties with a boolean expression e.
T{e} is a dependent type, that is, a type dependent on values. The type T is
called the base type and e is called the constraint. For reference types, the
constraint may specify the places at which the object resides.
For brevity, the constraint may be omitted and interpreted as true.
Constraints on properties induce a natural subtyping relationship: C{c} is
a subtype of D{d} if C is a subclass of D and c entails d.
Type parameters cannot be constrained.
[...]
4.5.3 Constraint semantics
STATIC SEMANTICS RULE (Variable occurrence): In a dependent type T =
C{c}, the only variables that may occur in c are (a) self, (b) properties
visible at T, (c) final local variables, final method parameters or final constructor
parameters visible at T, (d) final fields visible at T’s lexical place in
the source program.

I too would love to see something like this in Scala.

-0xe1a

Re: [scala] fixed array type

by Viktor Klang :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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

[scala] Re: fixed array type

by Jesper Nordenberg :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Paul Phillips wrote:

> On Tue, Jun 16, 2009 at 03:24:10PM -0700, Balthazar Crowley wrote:
>> 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.
>
> Oh, it sounds like you want a dependent type.
>
> That is, the type "Array[Char] of length 140." Right?
>
>   http://en.wikipedia.org/wiki/Dependent_type
>
> You can't have that in scala or any remotely mainstream language, but it
> is one of my fonder hopes for the future.

Actually I believe it can be done in C++ (C++ templates are after all
TC), which I consider a mainstream language. It can probably be done in
Scala as well (type projections are TC as well), but it would be
impractical given the current compiler status.

/Jesper Nordenberg


Re: [scala] fixed array type

by Landei :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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
Hi!

Does this count?
import scala.reflect.Manifest

    abstract class IntType{ val value:Int }
    
    class I42 extends IntType { val value = 42 }

    class FixedArray[V, I <: IntType](implicit manifest:Manifest[I]) {
       val length = manifest.erasure.asInstanceOf[Class[I]].newInstance.value
       val array = new Array[V](length)
       def apply(index:Int) = array(index)
       def update(index:Int, value:V) { array(index) = value }
    }

    val a = new FixedArray[String,I42]
    println(a.length)  //--> 42

Cheers,
Daniel

Re: [scala] Re: fixed array type

by Ricky Clarkson :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I though C++ standards only required something like 42 levels of
recursion in template expansion, making standard C++ templates not
actually Turing complete.

2009/6/17 Jesper Nordenberg <megagurka@...>:

> Paul Phillips wrote:
>>
>> On Tue, Jun 16, 2009 at 03:24:10PM -0700, Balthazar Crowley wrote:
>>>
>>> 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.
>>
>> Oh, it sounds like you want a dependent type.
>>
>> That is, the type "Array[Char] of length 140." Right?
>>
>>  http://en.wikipedia.org/wiki/Dependent_type
>>
>> You can't have that in scala or any remotely mainstream language, but it
>> is one of my fonder hopes for the future.
>
> Actually I believe it can be done in C++ (C++ templates are after all TC),
> which I consider a mainstream language. It can probably be done in Scala as
> well (type projections are TC as well), but it would be impractical given
> the current compiler status.
>
> /Jesper Nordenberg
>
>

Re: [scala] fixed array type

by Florian Hars-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Paul Phillips schrieb:
> You can't have that in scala or any remotely mainstream language

For varying values of mainstream (does "it's got an O'Reilly book" do?),
Oleg has a paper on how to do it in Haskell:

| We review several encodings of the numeric parameter but concentrate on the
| phantom type representation of a sequence of decimal digits. The decimal encod-
| ing makes programming with number-parameterized types convenient and error
| messages more comprehensible.
| ...
| Overall we demonstrate a practical dependent-type-like system that is just a
| Haskell library.

http://okmij.org/ftp/papers/number-parameterized-types.pdf

- Florian.