Forums

Explicit array sizes

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Explicit array sizes

Postby hopscc » Sun Jul 06, 2008 8:35 pm

Trying to make a Byte array for reading streams
The obvious thing
Code: Select all
buf = Byte[256]

gives " Explicit array sizes are not currently supported.'

Couldnt find anything else ( beyond assignment to a very large zeroed literal) that would give me an exactly sized array.

eventually ended up with a callout to c#
Code: Select all
buf = $sharp('new Byte[256]')

which worked fine.

Is there some cobra Language design decision or implementation problem/complication that makes providing sized arrays an issue or is this just a 'Not Yet Implemented' item?

What would need to be done ( and to what - high level) to support explicitly sized arrays
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Explicit array sizes

Postby hopscc » Sun Jul 06, 2008 8:49 pm

Grr ,
that second piece of code should of course be
Code: Select all
buf as Byte[] = $sharp(r'new Byte[256]')
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Explicit array sizes

Postby onodera » Mon Jul 07, 2008 10:56 am

I'd kill for old Basic array sizes and would make that declaration mandatory if I could. What they did in VB.NET was terrible, terrible...
Code: Select all
buf1 = Byte[1 to 256]
buf2 = Byte[0 to 255]

Of course that would with make debugging a torture or break CLS compatibility. :(
onodera
 
Posts: 8

Re: Explicit array sizes

Postby Charles » Mon Jul 07, 2008 9:43 pm

I think "buf = Byte[](256)" might work. Think of the general form "someVar = SomeType(args)". In this case, SomeType = Byte[] and the args are the initial size.

Step through the release notes and you'll find the notes on arrays when they were introduced. And/or since you run out of a workspace, check out the test cases:
Code: Select all
C:\...\Cobra\Workspace-New\Tests>cygfind . -name '*array*' | grep -v _svn
./110-basics-two/800-arrays
./110-basics-two/800-arrays/100-single-dim-array.cobra
./110-basics-two/800-arrays/110-slice-array.cobra
./110-basics-two/800-arrays/120-use-qualified-array-type.cobra
./240-generics/500-misc/110-array-is-ienumerable-of.cobra
./300-type-inference/420-from-array-String-split.cobra

I use generic lists almost exclusively, but I (barely) snuck in array support because so much of the .NET base class libraries use them. But I did not complete them. I would eventually like them to be on par with C#, but there's a million things to do...
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Explicit array sizes

Postby hopscc » Mon Jul 07, 2008 11:29 pm

OK - will check the rel notes and tests and go back read rather than skim src code ...
Just asked cos I thought you might already have thought about how where/how youd planned on adding this

The discussions I've read for Java ( and therefore presumably C#) specify an array as a contents type + length with
any initialiser being all or some part of the initial contents ( i.e the type includes the size in some fashion)

hence it looks more familiar to me for the size parameter to be inside the []
Code: Select all
buf = Byte[256]


I agree about the use of Lists and generic lists which conflicts somewhat with .NET's bias toward desire for
arrays Java is similar especially for IO. Figured it was just an unexamined historical progression from C's char/byte buffers.

Dont know VB/VB.NET but presume that syntax is an ugly progression from early MS Basics ( and others?) support for
programmatic 0-offset or 1-offset array indexing.

What other array support things does c# have in the language that cobra doesnt and you'd like to see (apart from this ) ?

I'll open a ticket for this.
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Explicit array sizes

Postby Charles » Mon Jul 07, 2008 11:35 pm

(1) I've never make an array higher than 1 dimension in Cobra.

(2) C# supports fixed length and dynamic arrays. Cobra only supports dynamic at this point.

(3) I've never gone through C# docs on arrays to see all the various features, errors and warnings around them. I'm probably missing some things.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Explicit array sizes

Postby hopscc » Thu Jul 24, 2008 6:11 am

Looks like I misunderstood your reply Chuck.
I took the description you gave of an array type initialiser (Type[](size) ) to be the construction syntax you were thinking of
rather than what had already been implemented :o

Unfortunately I didnt discover that until after I'd got a java/C# like syntax working :cry:
Code: Select all
buf as Byte[256]           # new
buf1 as Byte[] = Byte[](256) #existing

I'm goanna put two patches for this on ticket#27. One for the new (additional ) syntax If you want to take it and
the other for if you dont, correcting that @#$%^ error message to point to the assignment-to-arrayType-initialiserr syntax.
probably worthwhile to provide a working with arrays Howto as well

Looks like in addition to 1dim arrays c# provides multiDim and jagged - think the existing level of support ( 1dim arrays only) in cobra is sufficient for the moment.
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Explicit array sizes

Postby relez » Sat Aug 09, 2008 2:57 pm

Can i build an array of arrays?
relez
 
Posts: 69

Re: Explicit array sizes

Postby Charles » Sun Aug 10, 2008 4:49 pm

I haven't even tried. There are no test cases for doing so. You can build a list of lists, though:
m = List<of List<of number>>()

# you could wrap that in a "Matrix" class and overload indexing:

def [row as int, col as int] as number
return _data[row][col]

Again, I have nothing against multi-dim arrays, it's just that generic lists are good enough in many situations so arrays have not been a priority for me. In fact, the major reason I even added the single-dim ones early is that many parts of the .NET API expect them or return them (and sadly with no IEnumerable<of T> overload).

-Chuck
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 111 guests

cron