type-safe Methods

Behrang Saeedzadeh behrangsa at gmail.com
Mon Jun 20 22:09:22 PDT 2011


Juan (John?),

One good example to look at is the Lift framework for Scala. Using its
Mapper you can write type-safe queries such as:

import net.liftweb.mapper.By
val postsTitles: List[Post] = Post.findAll(By(Post.published, true))

For more info see here: http://www.assembla.com/spaces/liftweb/wiki/Mapper

Cheers,
Behrang Saeedzadeh
http://www.behrang.org



On Tue, Jun 21, 2011 at 12:06 AM,  <john at johnprieur.com> wrote:
> Behrang,
> Thank you for the explanation. I was not familiar with that proposal.
> I do not understand how the proposed feature would be useful.
>
> If yoru example code expresses this...
>   final Integer aNumber = 1;
>   Method<String> toString = Integer#toString();
>   System.out.println("toString = " + toString.invoke(aNumber));
> Then it seems to be equivalent to this...
>   System.out.println("toString = " + aNumber.toString());
>
> The point is that if you know at compile time that you want to invoke
> toString on an Integer object,
> what would be the use of doing that through reflection?
> On the other hand, if you need to determine the string name of a method at
> runtime,
> then you would not be able to use the "hard-coded" Class#methodName syntax .
> Either way, I don't see the use of the proposal.
>
> Thank you if you can offer a little more explanation.
>
> Juan
>
> -------- Original Message --------
> Subject: Re: type-safe Methods
> From: Behrang Saeedzadeh <behrangsa at gmail.com>
> Date: Mon, June 20, 2011 8:52 am
> To: juan at juanreza.com
> Cc: jdk8-dev at openjdk.java.net
>
> Juan,
>
> Right now in order to invoke a method on an object via reflection we
> have to write something like:
>
> final Integer aNumber = 1;
> final Class clazz = Integer.class;
> Method toString = clazz.getDeclaredMethod("toString");
> System.out.println("toString = " + toString.invoke(aNumber));
>
> This is not type-safe because we pass in the method name as a string
> to the getDeclaredMethod.
>
> Now let's pretend that Java had a method referencing construct symbol
> such as #. Then we could have achieved the same thing in a more
> concise and type-safe way:
>
> final Integer aNumber = 1;
> Method<String> toString = Integer#toString();
> System.out.println("toString = " + toString.invoke(aNumber));
>
> Cheers,
> Behrang Saeedzadeh
> http://www.behrang.org
>
>
>
> On Mon, Jun 20, 2011 at 10:14 PM, <juan at juanreza.com> wrote:
>> Behrang,
>> Please explain your idea. I am sorry that the JDK team did not understand
>> it. If you wish to discuss it, I might be able to help you.
>>
>> Juan
>>
>> -------- Original Message --------
>> Subject: Re: Any plans to support type-safe access to java.lang.Method?
>> From: Erik Trimble <erik.trimble at oracle.com&gt;
>> Date: Sun, June 19, 2011 4:53 pm
>> To: Behrang Saeedzadeh <behrangsa at gmail.com>
>> Cc: jdk8-dev at openjdk.java.net
>>
>> On 6/19/2011 5:07 AM, Behrang Saeedzadeh wrote:
>>> Hi all,
>>>
>>> Are there any plans to support type-safe access to methods?
>>>
>>> For example something like:
>>>
>>> java.lang.Method toString = Object#toString();
>>>
>>> It would make writing APIs like JPA 2's type-safe criteria builder
>>> more concise and readable.
>>>
>>>
>>> Cheers,
>>> Behrang Saeedzadeh
>>> http://www.behrang.org
>>
>> Behrang,
>>
>>
>>
>


More information about the jdk8-dev mailing list