Re: Java interop: casting

2011-06-17 Thread Mark Rathwell
Yeah, not sure which is better, Java's or C#'s varargs, but it does make
things nicer at times.  In C#, the method signature signature screams "I AM
passing an array", but you can pass arguments either way as in Java.

C#:

public void UseVarargs(params int[] args)
{
// Do something using argument array
}

public static void Main()
{
UseVarargs(1);
UseVarargs(1, 2, 3);
UseVarargs(new int[3] {1, 2, 3});
}

C#:

public void UseVarargs(params int[] args)
{
// Do something using argument array
}

public static void Main()
{
UseVarargs(1);
UseVarargs(1, 2, 3);
UseVarargs(new int[] {1, 2, 3});
}

Java:

public void useVarargs(int... args) {
// Do something using argument array
}

public static void main(string [] args) {
useVarargs(1);
useVarargs(1, 2, 3);
useVarargs(new int[] {1, 2, 3});
}



On Fri, Jun 17, 2011 at 1:35 PM, Gregg Reynolds  wrote:

> On Fri, Jun 17, 2011 at 10:17 AM, Mark Rathwell 
> wrote:
> >
> > In Java, varargs are actually converted to arrays at compile time.  It is
> > really just some syntactic sugar allowing you to use nicer syntax for
> array
> > arguments, and you can pass the arguments as an array, or as a comma
> > delimited sequence of arguments.
>
> Right, but the problem is when you pass just one thing (or nothing).
> It may get converted, but the syntax of the call declares loudly "I am
> NOT passing an array".  Atrocious language design, IMO, and it cost me
> several frustrating and wasted hours.  A minor point but good for a
> cheat sheet.
>
> -Gregg
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: Java interop: casting

2011-06-17 Thread Gregg Reynolds
On Fri, Jun 17, 2011 at 10:17 AM, Mark Rathwell  wrote:
>
> In Java, varargs are actually converted to arrays at compile time.  It is
> really just some syntactic sugar allowing you to use nicer syntax for array
> arguments, and you can pass the arguments as an array, or as a comma
> delimited sequence of arguments.

Right, but the problem is when you pass just one thing (or nothing).
It may get converted, but the syntax of the call declares loudly "I am
NOT passing an array".  Atrocious language design, IMO, and it cost me
several frustrating and wasted hours.  A minor point but good for a
cheat sheet.

-Gregg

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Java interop: casting

2011-06-17 Thread Mark Rathwell
In Java, varargs are actually converted to arrays at compile time.  It is
really just some syntactic sugar allowing you to use nicer syntax for array
arguments, and you can pass the arguments as an array, or as a comma
delimited sequence of arguments.


On Fri, Jun 17, 2011 at 10:57 AM, Gregg Reynolds  wrote:

> On Thu, Jun 16, 2011 at 6:12 PM, Stuart Halloway
>  wrote:
> >
> > Hi Gregg,
> > It appears that LocalServiceTestHelper's constructor takes an array
> of LocalServiceTestConfig. Try
> > (def bar (LocalServiceTestHelper. (into-array LocalServiceTestConfig
> [foo])))
> > Stu
>
> Hi Stu,
>
> Would you be the Stuart Halloway whose "Programming Clojure" book I
> bought last week?  Nice work!
> Using into-array indeed solved the problem - I even found it in the
> book, p. 85.  But if you're taking notes for the second edition this
> might be a good item for a "Java Interop Gotchas" list.  The method
> definition looks like this:
>
> public LocalServiceTestHelper(LocalServiceTestConfig... configs)
>
> I.e. it uses the varargs op ... instead of [].  The sample code uses:
>
> ... = new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
>
> i.e. it passes a single object, not an array.  The clojure code
> without into-array complains about bad class casting.  So it looks
> like there is a minor impedance mismatch between Java and Clojure when
> it comes to varargs, unless I missed something.  The rule seems to be:
> if the Java method takes varargs, pass it an explicit array.
>
> Thanks
>
> Gregg
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: Java interop: casting

2011-06-17 Thread Gregg Reynolds
On Thu, Jun 16, 2011 at 6:12 PM, Stuart Halloway
 wrote:
>
> Hi Gregg,
> It appears that LocalServiceTestHelper's constructor takes an array 
> of LocalServiceTestConfig. Try
> (def bar (LocalServiceTestHelper. (into-array LocalServiceTestConfig [foo])))
> Stu

Hi Stu,

Would you be the Stuart Halloway whose "Programming Clojure" book I
bought last week?  Nice work!
Using into-array indeed solved the problem - I even found it in the
book, p. 85.  But if you're taking notes for the second edition this
might be a good item for a "Java Interop Gotchas" list.  The method
definition looks like this:

public LocalServiceTestHelper(LocalServiceTestConfig... configs)

I.e. it uses the varargs op ... instead of [].  The sample code uses:

... = new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());

i.e. it passes a single object, not an array.  The clojure code
without into-array complains about bad class casting.  So it looks
like there is a minor impedance mismatch between Java and Clojure when
it comes to varargs, unless I missed something.  The rule seems to be:
if the Java method takes varargs, pass it an explicit array.

Thanks

Gregg

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: Java interop: casting

2011-06-16 Thread Stuart Halloway
Hi Gregg,

It appears that LocalServiceTestHelper's constructor takes an array of 
LocalServiceTestConfig. Try 

(def bar (LocalServiceTestHelper. (into-array LocalServiceTestConfig [foo])))

Stu

Stuart Halloway
Clojure/core
http://clojure.com

> Hi,
> 
> I'm trying to make the GAE local testing stuff (http://code.google.com/
> appengine/docs/java/tools/localunittesting.html)  work with Clojure
> and running into a cast problem.  Specifically, the example shows
> 
> private final LocalServiceTestHelper helper =
>new LocalServiceTestHelper(new
> LocalDatastoreServiceTestConfig());
> 
> where the LocalServiceTestHelper ctor takes one arg of type
> LocalServiceTestConfig (which is an interface).
> LocalDatastoreServiceTestConfig implements LocalServiceTestConfig.
> 
> which I try to do in clojure like this:
> 
> (def foo (new LocalDatastoreServiceTestConfig))
> (def bar (LocalServiceTestHelper. foo))
> 
> which yields a java.lang.ClassCastException.  Messing around with
> class, .getName, cast, etc. I end up with:
> 
> com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig
> cannot be cast to java.lang.Class
> 
> It seems that clojure won't treat LocalDatastoreServiceTestConfig as a
> LocalServiceTestConfig.  Am I interpreting all this correctly?  I'm
> relatively new to clojure and my java is a little rusty, but I've
> banged my head on this all afternoon and I'm out of ideas.
> 
> Thanks,
> 
> Gregg
> 
> 


-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Java interop: casting

2011-06-16 Thread Gregg Reynolds
Hi,

I'm trying to make the GAE local testing stuff (http://code.google.com/
appengine/docs/java/tools/localunittesting.html)  work with Clojure
and running into a cast problem.  Specifically, the example shows

 private final LocalServiceTestHelper helper =
new LocalServiceTestHelper(new
LocalDatastoreServiceTestConfig());

where the LocalServiceTestHelper ctor takes one arg of type
LocalServiceTestConfig (which is an interface).
LocalDatastoreServiceTestConfig implements LocalServiceTestConfig.

which I try to do in clojure like this:

(def foo (new LocalDatastoreServiceTestConfig))
(def bar (LocalServiceTestHelper. foo))

which yields a java.lang.ClassCastException.  Messing around with
class, .getName, cast, etc. I end up with:

com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig
cannot be cast to java.lang.Class

It seems that clojure won't treat LocalDatastoreServiceTestConfig as a
LocalServiceTestConfig.  Am I interpreting all this correctly?  I'm
relatively new to clojure and my java is a little rusty, but I've
banged my head on this all afternoon and I'm out of ideas.

Thanks,

Gregg

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en