Forums

Streams

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

Streams

Postby Charles » Fri Nov 28, 2008 1:36 pm

I have added streams to the language. Instead of saying IEnumerable<of T> you can say T* as in int*, String*, etc. This feature comes from the Comega language.

-- Streams are portable between backends, whereas .NET's IEnumerable<of> and JVM's Iterable<of> are not.

-- Streams are two-way compatible with IEnumerable/Iterable:
---- Streams can be used whereever a compatible IEnumerable/Iterable is expected.
---- An IEnumerable/Iterable can be used whereever a compatible stream type is expected.
---- The term "compatible" means the same inner type: int* is compatible with IEnumerable<of int>/Iterable<of int>, but not IEnumerable<of String>/Iterable<of String>

-- However, compatibility does not mean that you can use the methods of IEnumerable or Iterable, since these vary between platforms.

-- Streams are an abstract type, so you cannot create them directly with a call on the type such as `int*()`. Instead, use a concrete class such as `List<of>`, or `yield` results out of a method.

-- Streams are defaulted to be empty rather than nil.

-- You can look at the test cases online or via your workspace (which will be nice with syntax highlighting):

Tests/240-generics/500-misc/800-streams.cobra

Tests/240-generics/500-misc/810-streams-errors.cobra

If you have any questions or find any problems, let me know.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Streams

Postby Charles » Fri Nov 28, 2008 5:22 pm

Two of the more common things you do with streams are:
-- loop through them with "for"
-- make a list out of them so you can do list stuff like sort, reverse, etc.

class Example

def main is shared
pass

def foo(ints as int*)
t = List<of int>(t)
t.sort
t.reverse
print t

def bar(ints as int*)
t = for i in ints where i > 0
print t

def bill(customers as Customer*)
for c in customers
c.billIfNeeded

Note that you can pass a stream, list, array or IEnumerable/Iterable wherever a compatible stream is expected (one with the same inner type). Methods that use the stream type for arguments can therefore be used in a wider range of situations as compared to expecting a more specific type such as array. Of course, sometimes you want something more specific and in those cases you should feel free to use List<of T>, IList<of T>, T[], etc.

HTH
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Streams

Postby relez » Sun Nov 30, 2008 8:40 am

It seems that this feature might be useful in order to obtain a better portability of language. Is this the main reason? But i think that programs this way will be also more readable....
relez
 
Posts: 69

Re: Streams

Postby Charles » Sun Nov 30, 2008 9:09 am

Portability and readability were both motivations. Yes, portability was the main motivation that caused me to prioritize this item now. Recall the post on cues as well:
cue enumerate as T*
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 18 guests

cron