How to handle Collection.toArray()

58 views
Skip to first unread message

Tako Schotanus

unread,
May 14, 2013, 6:59:51 PM5/14/13
to ceylon-dev
Ok, I need some help figuring this out.

I want to implement Java's Collection interface on a Ceylon object that wraps a Ceylon Iterable like this:

    shared class JavaCollection<T>({T*} items) satisfies JCollection<T> { }

But there's one method that's giving me trouble, in the Java interface it's:

    Object[] toArray()

which gets translated in Ceylon to:

    shared actual ObjectArray<Object> toArray() { }

I'm trying something like this:

    shared actual ObjectArray<Object> toArray() => arrays.toObjectArray(items);

but obviously the the toObjectArray() method returns a T[], not an Object[].
I thought I could maybe change the signature to return ObjectArray<Anything> but that can't work either.

So is there a way to make this work in Ceylon or am I stuck with implementing this in Java?

-Tako

Stephane Epardaud

unread,
May 15, 2013, 4:13:17 AM5/15/13
to ceylon-dev
Did you try arrays.toObjectArray<Object>(items)?


--
You received this message because you are subscribed to the Google Groups "ceylon-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ceylon-dev+...@googlegroups.com.
To post to this group, send email to ceylo...@googlegroups.com.
Visit this group at http://groups.google.com/group/ceylon-dev?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Stéphane Épardaud

Tako Schotanus

unread,
May 15, 2013, 4:15:22 AM5/15/13
to ceylon-dev
Yes, it will then complain: argument must be assignable to parameter values of toObjectArray: {T*} is not assignable to {Object*}


-Tako

Stephane Epardaud

unread,
May 15, 2013, 4:16:43 AM5/15/13
to ceylon-dev
arrays.toObjectArray<Anything>(items)?

Tako Schotanus

unread,
May 15, 2013, 4:20:34 AM5/15/13
to ceylon-dev
specified expression type must be assignable to declared return type: ObjectArray<Anything> is not assignable to ObjectArray<Object>



-Tako

Stephane Epardaud

unread,
May 15, 2013, 4:23:47 AM5/15/13
to ceylon-dev
Interesting. This may be related to the model loader and our mapping of types, because in theory it shouldn't even be ObjectArray<Object> but ObjectArray<Object?>, no?

Tako Schotanus

unread,
May 15, 2013, 4:30:42 AM5/15/13
to ceylon-dev
Right, it seems we're being too strict in our mapping. But I'm not sure where/how exactly it should be relaxed.

But even though ObjectArray<Object?> should definitely be possible it still wouldn't solve this problem.

I guess Gavin will have to take a look at this.



-Tako

Stephane Epardaud

unread,
May 15, 2013, 4:34:32 AM5/15/13
to ceylon-dev
What about:

 shared actual ObjectArray<Anything> toArray() => arrays.toObjectArray<Anything>(items);

Tako Schotanus

unread,
May 15, 2013, 4:43:56 AM5/15/13
to ceylon-dev
No, I mentioned that in the original mail, the typechecker doesn't think that implements the interface.


-Tako

Stephane Epardaud

unread,
May 15, 2013, 4:46:55 AM5/15/13
to ceylon-dev
So we have a more general problem with interop and type parameters then. Because when a Java interface says it returns X<Y> it may mean any combination of X<Y>?, X<Y?>?, X<Y> or X<Y?> in reality. I'm pretty sure we already support X<Y>? and X<Y> but not the rest.

Tako Schotanus

unread,
May 15, 2013, 4:54:03 AM5/15/13
to ceylon-dev
Yes, the last one we support.

This is first a spec issue, right? With possibly/probably repercussions in the compiler.
So shall I open an issue for it on spec?


-Tako

Stephane Epardaud

unread,
May 15, 2013, 4:56:27 AM5/15/13
to ceylon-dev
This is a jvm compiler issue. It's mostly about the model loader, though it requires a bit of support in the typechecker to let things pass. Perhaps we can generalise the current solution for java types and optionality.

Ross Tate

unread,
May 18, 2013, 9:36:24 PM5/18/13
to ceylo...@googlegroups.com
I think arrays.toObjectArray<T> should accept a {T?*} rather than a {T*} (and still return an ObjectArray<T>).

Gavin King

unread,
May 19, 2013, 1:18:23 PM5/19/13
to ceylo...@googlegroups.com
That looks right to me, assuming ObjectArray<T> is an Iterable<T?>, which would be the correct thing...

Sent from my iPhone

Stéphane Épardaud

unread,
May 19, 2013, 2:14:04 PM5/19/13
to ceylon-dev
It is not Iterable.

Sent from my phone

Gavin King

unread,
May 19, 2013, 2:20:54 PM5/19/13
to ceylo...@googlegroups.com
OK then what I mean is assuming an element can be null.

Sent from my iPhone
Reply all
Reply to author
Forward
0 new messages