casting from java.util.List to CAL's list

17 views
Skip to first unread message

Karel Gardas

unread,
Sep 16, 2010, 3:51:16 PM9/16/10
to CAL Language Discussion
Hello,

while playing with CAL I've found one point over which I'm not able to
get and that's a need to cast from JList to CAL's internal list. I'm
attempting something like:

[code]
foreign unsafe import jvm "cast"
public castEListToListOfEReferences :: EList -> [EReference];
[/code]

or

[code]
foreign unsafe import jvm "cast"
public castEListToListOfEReferences :: EList -> List;
[/code]

but I always get error along the line of:

Type [Processor.EReference]/List is not supported for foreign calls.
BTW: both EList and EReference are constructs from Eclipse EMF. EList
is just child of java.util.List...

EList is imported as:
data foreign unsafe import jvm public
"org.eclipse.emf.common.util.EList"
public EList deriving Inputable, Outputable;

and EReference as:
data foreign unsafe import jvm public
"org.eclipse.emf.ecore.EReference"
public EReference deriving Inputable, Outputable;

Do I make any mistake in the code above? Is there any trick how to
write this cast properly? Any help is appreciated here,

Thanks!
Karel

Tom Davies

unread,
Sep 16, 2010, 8:36:52 PM9/16/10
to CAL Language Discussion
If I understand what you want to do, you need to cast your List
subclass to a CAL JList and then use inputList (example below) -- let
me know if you need any more assistance.

Tom

Test.cal:

module Org.Kablambda.Test;

import Cal.Core.Prelude using
typeClass = Inputable, Outputable;
typeConstructor = Int, JList;
;

import Cal.Collections.List using
function = inputList;
;

data foreign unsafe import jvm public
"org.kablambda.cal.utils.MyList"
public MyList deriving Inputable, Outputable;

data foreign unsafe import jvm public
"org.kablambda.cal.utils.MyElement"
public MyElement deriving Inputable, Outputable;

foreign unsafe import jvm "cast"
public castEListToListOfEReferences :: MyList -> JList;

foreign unsafe import jvm "static method
org.kablambda.cal.utils.MyList.make"
public jMake :: Int -> MyList;

make :: Int -> [MyElement];
make n = inputList $ (castEListToListOfEReferences $ jMake n);

MyList.java:


package org.kablambda.cal.utils;

import java.util.ArrayList;

public class MyList extends ArrayList<MyElement> {
public static MyList make(int n) {
MyList l = new MyList();
for (int i = 0; i < n; ++i) {
l.add(new MyElement());
}
return l;
}

}

MyElement.java:


package org.kablambda.cal.utils;

public class MyElement {

}

Richard Webster

unread,
Sep 17, 2010, 12:49:21 AM9/17/10
to cal_la...@googlegroups.com
You should also be able to eliminate the cast to JList by doing the following:

make :: Int -> [MyElement];
make n = input $ output $ jMake n;

Here, the output function just acts to cast MyList to JObject.
Then, the basic input function can be used.
The list implementation of input is quite flexible, and will handle a
number of common Java collections, arrays, iterators, etc.

Thanks
Rich

> --
> You received this message because you are subscribed to the Google Groups "CAL Language Discussion" group.
> To post to this group, send email to cal_la...@googlegroups.com.
> To unsubscribe from this group, send email to cal_language...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/cal_language?hl=en.
>
>

Karel Gardas

unread,
Sep 21, 2010, 3:28:23 AM9/21/10
to CAL Language Discussion

Guys,

thank you very much for your `input' and `inputList'. Indeed, that's
what needed. While looking around I've also found input with kind of
strange syntax to me:

(input :: T1 -> T2), e.g. (my case):
(input :: JObject -> [EReference]) (castEListToJObject (getEReferences
(getEClass book)));

I'll need to verify if castEListToJObject is useless and I can use
directly:
(input :: EList -> [EReference]) (getEReferences.....)

or not.

anyway, this thing is working now. Thanks!
Karel

On Sep 17, 6:49 am, Richard Webster <rdwebs...@gmail.com> wrote:
> You should also be able to eliminate the cast to JList by doing the following:
>
> make :: Int -> [MyElement];
> make n = input $ output $ jMake n;
>
> Here, the output function just acts to cast MyList to JObject.
> Then, the basic input function can be used.
> The list implementation of input is quite flexible, and will handle a
> number of common Java collections, arrays, iterators, etc.
>
> Thanks
> Rich
>

Richard Webster

unread,
Sep 21, 2010, 9:49:00 AM9/21/10
to cal_la...@googlegroups.com
Hi Karel,

The input function always takes a JObject argument, just as output
always returns a JObject.
input :: JObject -> a;
output :: a -> JObject;

There are a number of ways to write the code here, but they all end up
doing the same work.
The following should work:
input (castEListToJObject (getEReferences (getEClass book)));
Here castEListToJObject is needed to make the types match up in CAL.

Thanks
Rich

Reply all
Reply to author
Forward
0 new messages