|
View:
New views
14 Messages
—
Rating Filter:
Alert me
|
|
|
[scala] fixed array typeAll,
Does Scala have a fixed array type?
Regards, Balthazar -- Balthazar Crowley Resident Magician DSLver.com |
|
|
Re: [scala] fixed array typeOn Tue, Jun 16, 2009 at 9:24 PM, Balthazar Crowley <balthazar.crowley@...> wrote: All, 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?
-- Viktor Klang Scala Loudmouth |
|
|
Re: [scala] fixed array typeViktor,
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:
-- Balthazar Crowley Resident Magician DSLver.com |
|
|
Re: [scala] fixed array typenew Array[Char](140) ?
--j On Tue, Jun 16, 2009 at 2:39 PM, Balthazar Crowley <balthazar.crowley@...> wrote: Viktor, |
|
|
Re: [scala] fixed array typeJorge,
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) ? -- Balthazar Crowley Resident Magician DSLver.com |
|
|
Re: [scala] fixed array typeOn 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 typePaul,
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:
-- Balthazar Crowley Resident Magician DSLver.com |
|
|
Re: [scala] fixed array typeYet another side of lambda cube...
2009/6/16 Paul Phillips <paulp@...>
-- Thanks, -Vlad |
|
|
Re: [scala] fixed array typeOn 16-Jun-09, at 3:32 PM, Paul Phillips wrote:
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:
[...]
I too would love to see something like this in Scala. -0xe1a |
|
|
Re: [scala] fixed array typeBut 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, -- Viktor Klang Scala Loudmouth |
|
|
[scala] Re: fixed array typePaul 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 typeHi! 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 typeI 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 typePaul 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. |
| Free embeddable forum powered by Nabble | Forum Help |