Forums

Foo<of T>

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

Foo<of T>

Postby Charles » Mon Jul 09, 2012 11:24 pm

jaegs wrote:Similarly, why is the "of" keyword required in generics?
List<of int>

It seems obvious to me that whatever is inside the angle brackets is a type and no other decoration is necessary. Plus, I imagine really long generics could get messy (well, messier than they already are)
IEnumerable<of List<of Dictionary<of string, int>>>


What should the parser presume when it processes the following?
a = B < C ...

Is that the start of a generic or a comparison between B and C?

So "of" is to eliminate ambiguity with the less-than operation which is "<". Of course, we know this is not strictly necessary because C# does not take this approach. So I took a peek at someone's C# parser to see how they did it. There was something hackish in the parser to pull it off. The rest of the grammar was parsed in a straightforward manner, but then "B < C" required some major back bending. And maybe we could do the same, but the C# parser got me thinking: "it really is kind of ambiguous". Also, it may be more difficult for Cobra because types can be directly referenced:
t = List<of int>
# in C#: t = typeof(List<int>)

But then maybe we could get into casing: Cobra requires types to be capped while params and locals must start lower cased. Throw in the fact that classes cannot be compared for ordering and we could have our disambiguation approach right here.

Although Cobra can't push the casing restrictions on .NET libs produced in other languages. On the other hand, it's rare to see a lower cased generic class in a library and "<of" could be the fallback for using such poorly styled libraries.

So there are possibilities that could be explored for implementing it in the Cobra parser.

How do people feel about "of"? Don't care? Like it? Dislike it?

Usually such questions are problematic as you'll mostly get answers from the discontents who may be outnumbered by the contents who aren't energized enough to speak up. In any case, it's open to discussion.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Foo<of T>

Postby hopscc » Tue Jul 10, 2012 5:39 am

FWIW I'm in the don't-care to like band.

I think it reads/scans better, clearer and it disambiguates and simplifies the tokeniser/parser coding(as stated)
Personally I see it also as clarifying :D (with 'of' as a readable delimiter/separator)
the sea of punctuation ('<', '>') and typenames you get with complex
Java/C# Generic descriptions..

OTOH its a bit of a visual/recognition cognitive dissonance stepping from C# and Java code across to the cobra equivalent
(my own personal Imp of Perversity is now screaming at me, that along with so many cobra differences, this is a good thing.... )

But then again I've always felt the <TYPE> syntax for generics in Java/C# is an abomination......
hopscc
 
Posts: 632
Location: New Plymouth, Taranaki, New Zealand

Re: Foo<of T>

Postby jaegs » Tue Jul 10, 2012 2:48 pm

Is it also possible to have
t = of List<int>
# in C#: t = typeof(List<int>)


where the keyword "of" is shorthand for typeof()?
jaegs
 
Posts: 58

Re: Foo<of T>

Postby nerdzero » Tue Jul 10, 2012 3:39 pm

I like having "of" in there. It reads closer to the way I think about the types such as "What is this? Oh, it's a list of ints." or "Hmm, I should really declare this as a list of Nodes". Doesn't quite match the way I think about dictionaries but I can live with that.
nerdzero
 
Posts: 286
Location: Chicago, IL

Re: Foo<of T>

Postby Charles » Wed Jul 11, 2012 11:31 am

Actually thinking with "of" works for me for Dictionaries too because I see them as a "set of key+value pairs".

In fact, in .NET, Dictionary<of TKey, TValue> implements ICollection<KeyValuePair<TKey, TValue>> and IEnumerable<KeyValuePair<TKey, TValue>>.

I think Smalltalk presented them in a similar fashion too.

In Cobra, you can loop through dictionaries like so:

for pair in {'x': 1, 'y': 2}
print pair
print pair.key
print pair.value


For the record, you can also unpack the pair immediately:

for key, value in {'x': 1, 'y': 2}
trace key, value
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Foo<of T>

Postby nerdzero » Wed Jul 11, 2012 12:59 pm

Charles wrote:Actually thinking with "of" works for me for Dictionaries too because I see them as a "set of key+value pairs".

Okay, that makes sense. But I think of a Dictionary<of String, int> as a "set of ints keyed by Strings". However, I doubt many people besides me would like writing any of the following:

Dictionary<of int key by String>
Dictionary<of int from String>
Dictionary<of String to int>

This is just the way I think of dictionaries/maps though. For a true pair I think Pair<of Foo, Bar> is just fine and dandy. So, if you think of dictionaries as a set of pairs, then SomeKindOfCollection<of KeyValuePair<of TKey, TValue>> makes perfect sense :)
nerdzero
 
Posts: 286
Location: Chicago, IL

Re: Foo<of T>

Postby kirai84 » Thu Jul 19, 2012 3:59 am

Afair, in Scala and IronRuby this made somewhat better yet unusual:
Dictionary[Int, String]

Readable, compact, easy to parse.
kirai84
 
Posts: 24

Re: Foo<of T>

Postby kirai84 » Thu Jul 19, 2012 4:03 am

And also there could be
l = List of Int
d = Dictionary of(Int, String)
kirai84
 
Posts: 24

Re: Foo<of T>

Postby Charles » Fri Jul 20, 2012 10:46 am

When I was first designing the Cobra syntax, I considered the "List[int]" bracket syntax briefly, but brackets were already used for indexing objects and slicing:
text = text[:100]

item = list[0]

class Foo

get [i as int] as int
return i * 2 # silly example

Granted, if you block indexers from being declared as static/shared then you can use that syntax, but I didn't care for overloading square brackets in this way.

I came very close to using "List of int". Easy to type and read; looks nice in declarations:

class Foo

def bar(stuff as List of Item)
pass


But I disliked what it looked like with > 1 type args as in the Dictionary case and also when combined with further types:

class Foo

var _bar as List of Dictionary of (int, String)

var _baz as Dictionary of (int, List of String)


The current syntax makes the type look more atomic/self-contained with the closing `>`. Of course, this is very subjective.

Thanks for commenting.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Foo<of T>

Postby kirai84 » Fri Jul 20, 2012 5:51 pm

If i'm not mistaken, neither C# nor Java allows static indexers (not sure about indexers in Java actually).

I do think that this:
class Foo

var _bar as List of Dictionary of (int, String)

var _baz as Dictionary of (int, List of String)

looks much cleaner and clearer than this (and yet has less brackets):
class Foo

var _bar as List<of Dictionary<of int, String>>

var _baz as Dictionary<of int, List<of String>>

Also these both could be worked around with aliases (haven't found the syntax for aliases in Cobra):
use ListOfDictionaries as List of Dictionary of (int, String)
use DictionaryOfLists as Dictionary<of int, List<of String>>

Using of aliases in such cases is always a good solution anyway.

Still I do agree that any changes to the syntax should be done only if there is a huge demand for them and if the author of the language do agree with them (an author should like his language :D). And these things really are more subjective than they matter. I dislike angle brackets still I use(d) them in C++, C# and VB.NET.

What would be much better is to have aliases like these in the language:
use SomeAlias<of T> as Some.Freaking.Long.Named.Type<of T>
use IntDictionary<of T> as Dictionary<of int, T>

I miss them so much in C# :cry: .

Thanks for answering.
kirai84
 
Posts: 24

Next

Return to Discussion

Who is online

Users browsing this forum: No registered users and 13 guests