Re: Tuples, Generics, and compiler bugs, oh my!
Daniel Bonniot <[email protected]> Sun, 10 Jul 2005 11:25:39 +0200
| Newsgroups | gmane.comp.lang.nice.general |
|---|---|
| Message-ID | <[email protected]> |
> I was attempting to create a useful toString() method for Tuples. So I started
> by writing the following:
>
> String toString(( <T>t,<U> u))
> {
> return "("+t.toString()+","+u.toString()+")";
> }
>
> But that did not work. Apparently generics within tuples don't work.
They do, it's just a problem a syntax. Type parameters are always introduced
at the head of the method declaration:
<T,U> String toString(( T t, U u )) = "(" t "," u ")";
Note that you can use + for string concatenation if you are more comfortable
with it. To use toString, you would need to guarantee that the tuple elements
are not null (which is possible with <!T,!U> but it's less general).
> Please locate the bug that caused the problem. What is "unknown" anyway?
I think you are using an old version. I tried with 0.9.11, and I can't
reproduce the exception. "unknown" does not exist anymore (it was used for
unknown type parameters, especially with imported Java code, but not a single
'?' is used, consistently with generic wildcards).
> Why
> does "encountering a '<' not match a '<IDENT>'"?
JavaCC uses <IDENT> to mean "a token of kind IDENT", that is, "an identifier".
> Can we make it possible to
> enable generics within tuples?
Yes, it is possible ;-)
> And finally: Can we make some mechanism for using tuples as more general data
> type and write code that can deal with tuples of arbitrary length,
I suppose we could add a type nice.lang.Tuple and make all tuple types
subtypes of it. Then nice.lang could have a method:
?Object[] elements(Tuple t) //= cast(t);
With this, you could write methods that work an any tuple, like toString.
How does this sound?
Daniel
-------------------------------------------------------
This SF.Net email is sponsored by the 'Do More With Dual!' webinar happening
July 14 at 8am PDT/11am EDT. We invite you to explore the latest in dual
core and dual graphics technology at this free one hour event hosted by HP,
AMD, and NVIDIA. To register visit http://www.hp.com/go/dualwebinar