Re: Tuples, Generics, and compiler bugs, oh my!
Tom Kaitchuck <[email protected]> Sun, 10 Jul 2005 14:46:14 -0700
| Newsgroups | gmane.comp.lang.nice.general |
|---|---|
| Message-ID | <[email protected]> |
On Sunday 10 July 2005 2:25 am, Daniel Bonniot wrote:
> > 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).
Ok, I'm using the following:
String toString( (int t, int u))
{
return "HELLO: (" + t.toString() + "," + u.toString() + ")";
}
<T,U> String toString(( T t, U u )) = "(" t "," u ")";
void main(String[] args)
{
int a = 1;
int b = 2;
println((a,b).toString());
}
and that prints:
[I@df6ccd
Why aren't ether of these functions getting called?
> > 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).
Yes I was using 0.9.10 which I believed to be current, as it is still linked
to from the installation page for the rpm, even though 0.9.11 is on
sourceforge.
> > 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?
Actually I gave this some thought, and the conceptual way I can think of
dealing with this is to eliminate the concept of tuples and replace it with
the idea of nested couples. (Internal code would remain largely the same...)
Just explain that:
(int i, String s, Cat c, int j)
Is actually shorthand for:
(int i, (String s, (Cat c, int j)))
Then it would be easy to write function for arbitrary length tuples:
<T> void Print((int i, String s, ?T ext = null)) = System.out.println(i s
Print(ext)); //Would get picked first because it is more specific.
<T> void Print((Object o, ?T ext = null)) = System.out.println(j
Print(ext)); //Would get picked secound because it is more specific than the
third.
<T> void Print((?T lastElement)); //Picked last.
Then optional and named parameters would work as normal. In fact you could
even say that tuples were just an extention of normal arguments and eliminate
the extra set of parentheses in the function definition, if all it took was a
single n-tuple.
-------------------------------------------------------
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