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".

Re: Foo<of T>

Postby Charles » Fri Jul 20, 2012 8:07 pm

Now I recall more of what I didn't like about the "Dictionary of (int, String)". It was object creation:
d = Dictionary of (int, String)()

In fact, early versions of Cobra used "List(of int)" but then I made this mistake all the time:
someList = List(of int)  # expecting a list, but really made a reference to a type

Something about the presence of the parens made be think the subsequent parens for the call were already there. When I switched to angle brackets, I stopped doing that:
someList = List<of int>()

I think this was before Cobra was widely announced in 2008.

Regarding aliases, I don't have anything against them, but haven't needed them much and often get by with:
class FooCache inherits Dictionary<of int, List<of String>>
pass

This can also lead to adding convenience methods to the class and making the code even more readable.

But I can still see how aliases would be useful and in some cases such as name collisions between namespaces, very useful. I would prefer this syntax:
alias SomeFoo<of T> = Some.Freaking.Long.Named.Type<of T>
alias IntMap<of T> = Dictionary<of int, T>
# form:
alias NewAlias = ExistingNameSpaceOrType
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Foo<of T>

Postby kirai84 » Sat Jul 21, 2012 3:55 am

The syntax of class instantiating seems somewhat inconsitent to me. Why not use
aType = ref List<of int>
to get a type reference and
aList = List<of int>
to get a list. In according to the method usage and due to the same reasons.
And now you can use this syntax if you'd like:
someList = List(of int) # You get what you expect.


I totally agree on alias syntax.
While I miss such aliases in C# mostly due to lack of type inference for field's/propertie's declarations, I would definitly make use of them in Cobra too.
class FooCache inherits Dictionary<of int, List<of String>>
pass
is not equivalent to
alias FooCache = Dictionary<of int, List<of String>>
cause the following is false:
typeof(FooCache) ==  typeof(Dictionary<of int, List<of String>>)
and sometimes it matters.

"Use" and "alias" clauses can be useful in a local context too. Like this:
def someMethod
use System.Windows.Forms
# Can be useful when you write your own controls library with names clashing with WinForms control's names
# and using only the Form and few other types from WinForms in few methods to create windows.

alias V = Some.Long.TypeName
v = V(k.someLongNamedField1, i.someLongNamedField2, r.someLongNamedField3, a.someLongNamedField4)
Like in Ada.

Also there is a keyword "global" in C# wich can be useful in some cases of name collision wich can't be resolved by another means sometimes. Though they are rear times. I haven't used it once yet.
kirai84
 
Posts: 24

Re: Foo<of T>

Postby jaegs » Sat Jul 21, 2012 6:50 pm

class FooCache inherits Dictionary<of int, List<of String>>
pass

The above would not work if trying to alias a C# sealed class (and in the future, a Java final class). I really like the idea of renaming ugly generics
alias SomeFoo<of T> = Some.Freaking.Long.Named.Type<of T>

As this is a big hole in C#.

There is also the issue of namespace conflicts.
jaegs
 
Posts: 58

Re: Foo<of T>

Postby Charles » Sat Jul 21, 2012 11:40 pm

Note that using parens for generics still looks awkward imo when there are arguments to the initializer:
someList = List(of int)(initCapacity)
p = Pair of (int, int)(1, 1)

The idea of using 'ref' for referring to classes is an interesting one since we do that with methods. However, I have gotten use to referring to classes directly as you can do in Python. Combined with the fact that I don't using parens for generics is very readable, I'm not motivated to change it.

Btw I understand everyone's points about subclasses not being the same as aliases, name collisions, etc. which is why I wrote "But I can still see how aliases would be useful and in some cases such as name collisions between namespaces, very useful." Thanks for enumerating the details and providing explicit examples.

Re C#'s global, I've used it maybe two times in years of C# coding... but when I needed it, I needed it.

Last, but least, I have learned the hard way you can only make a good syntax. You cannot make a perfect one.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Foo<of T>

Postby chrisaj » Wed Aug 22, 2012 6:29 am

I wonder if this "of" have to be inside angle brackets?
In my opinion it would look much better if you could write
foo = List of <T>
foo = List of T # shorthand for above
bar = Dictionary of <string, int>

Instead
foo = List<of T>
bar = Dictionary<of string, int>
chrisaj
 
Posts: 1

Re: Foo<of T>

Postby Charles » Mon Aug 27, 2012 2:32 pm

Sorry I did not see the post above by chrisaj for some reason (first posts have to be approved; this protects us from spam).

Note that the assignments in your examples are of types rather than instances:
# type assignement
dictType = Dictionary of <string, int>
listType = List of <int>
# obj assignement
dict = Dictionary of <string, int>()
stuff = List of <int>(otherList)

I found this thread useful for exploring syntax ideas regarding generics. I think think that all things considered, I'm satisfied with the status quo.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Previous

Return to Discussion

Who is online

Users browsing this forum: No registered users and 10 guests

cron