Issue Details (XML | Word | Printable)

Key: BOO-803
Type: Improvement Improvement
Status: Open Open
Priority: Minor Minor
Assignee: Unassigned
Reporter: Avishay Lavie
Votes: 1
Watchers: 2
Operations

If you were logged in you would be able to see more operations.
Boo

Generic list and hash builtins

Created: 22/Feb/07 03:35 PM   Updated: 26/Jul/08 04:01 PM
Component/s: Runtime (Boo.Lang)
Affects Version/s: None
Fix Version/s: 0.9

Time Tracking:
Not Specified

Issue Links:
Related
 


 Description  « Hide
We should provide a generic version of list and hash builtins, with type inference for list/hash literals.

l = [1,2,3]
l.GetType() is typeof(list of int)

h = {1: "Hello", 2: "World"}
h.GetType() is typeof(hash[of int, string])

Type inference can be overridden, like with array literals:

l = [of object: 1,2,3]
l.GetType() is typeof(list of object)

h = {of object, string: 1: "Hello", 2: "World"}
h.GetType() is typeof(hash[of object, string])



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Hans-Christian Holm added a comment - 23/Feb/07 05:46 AM
What about

l = [1,2,3] of object

h = {1: "Hello", 2: "World"} of object, string


Avishay Lavie added a comment - 23/Feb/07 06:01 AM
Oh, much better! Bamboo, your opinion?

George Dernovoy added a comment - 28/Jul/07 01:20 PM
It would be great to realize both proposal: type inference by default and explicit typification.
Quite good if generic hash and list are System.Collection.Generic.List and Dictionary or
their childs.

Rodrigo B. de Oliveira added a comment - 26/Jul/08 11:43 AM
I like it.

Rodrigo B. de Oliveira added a comment - 26/Jul/08 11:43 AM
I'll start by extracting a GenericList class out of Boo.Lang.List, I want to use the functionality there with generics. Let's see.

Avishay Lavie added a comment - 26/Jul/08 12:12 PM
Why not use the original System.Collections.Generic.List? Most of the extra behavior of Boo's list can be achieved via extension methods/properties, except maybe negative indexing, which we want to separately add as to any IList, anyway.

In fact, most of the Boo.Lang.List extra behavior (like AddUnique, Reverse, FindAll etc.) should be added via extensions to IList or ICollection, not to some specific implementation. Some of it already exists as .NET 3.5 extensions.

I really don't think there's a good cause to introduce new collections. Avoiding it will save users from having to repeatedly convert ILists into Boo lists and vice-versa.


Rodrigo B. de Oliveira added a comment - 26/Jul/08 12:57 PM
For now I'm just extracting a generic base class from List. If all goes well, List will simply become an alias to the new List[of T] type.

There are 3 things that I'm not sure how to properly support with the std generic list, you might want to comment:

  • value based equality (Equals should return true if all the elements are equal (including nested arrays, etc))
  • hashcode based on all the elements
  • nicer ToString representation

Avishay Lavie added a comment - 26/Jul/08 02:48 PM
1. value-based Equals is problematic, considering arrays don't natively support it (or do they?). It won't make much sense if [(1,2,3)].Equals([(1,2,3)]) returned true but (1,2,3).Equals(1,2,3) returned false.

2. You can achieve all of these with inheritance, but perhaps it'll be better to make Boo's list a wrapper for any IList<T>, and provide a default constructor which uses a new internal List<T>. That way you can transform an IList<T> into a boo list in O(1).

3. The rest of the functionality should still be applied to IList<T> via extensions, in my opinion.

4. Keep in mind that most of this functionality is present (and possibly better supported) in the .NET Framework 3.5.


Rodrigo B. de Oliveira added a comment - 26/Jul/08 03:10 PM
The == operator preserves value semantics for arrays. if you call the Equals method you get the runtime view of equality...

Your point 3 is a very interesting idea indeed.


Avishay Lavie added a comment - 26/Jul/08 03:38 PM
That's confusing. I thought Boo's == operator translates directly to C#'s Equals operator...
Does it work dynamically?
arr1 as object = (1,2,3)
arr2 as object = (1,2,3)
assert  arr1 == arr2

Rodrigo B. de Oliveira added a comment - 26/Jul/08 04:01 PM
It's not confusing when you know the rules

boo's equality operator preserves equality semantic for arrays, lists and hashes.

Enter boo code in the prompt below (or type /help).
>>>a1 as duck = (1, 2, 3)
(1, 2, 3)
>>>a2 as duck = (1, 2, 3)
(1, 2, 3)
>>>a1 == a2
true
>>>a1 is a2
false
>>>