Glue classes proposal 0.9
Marek Kozieł
develop4lasu at gmail.com
Sun Mar 22 15:10:42 PDT 2009
2009/3/22 Joseph D. Darcy <Joe.Darcy at sun.com>:
> From this proposal, I do not understand what you are trying to propose.
>
> -Joe
>
>
2009/3/22 <rssh at gradsoft.com.ua>:
>
> May be. Different people think in different way.
> For me, glue classes is 'traits' (i.e. compositivie units of behaviour)
> mixed with 'extension methods'. I. e. reading you proposal I think 30
> seconds, that say 'o, traits with extension method'. If word 'like traits
> wich can be bound to classes as extension methods' was in the first
> paragragh that time to understand such proposal for me, was 3 sec. instead
> 30. But this is only as my head work (I can tell you what is hard to
> understand only from my introspection, sorry). Other people think in
> different way, so may be functionality of glue classes is bound to such
> terms only for me. Other people will tell you what hard for them.
>
> Yet one thing, which can help: add more concrete example and describe
> in detail process of method call (if method call is changed on JVM level)
> or compiling to traditional java source code (if method call does not
> changed on JVM level).
>
> For example, I can't understand - how JVM or compiler will locate trait
> with such function signature during method call ? By building dependency
> 'glue dependency graph' and collecting candidated in all possible glue
> classes ?
>
>
I'll follow full process then.
Let's say, we write sample program doing something...
Part 1 (we have already some glue classes defined)
class Some<T>{
T[] elements;
}
Now, we need all elements greater than given one
import java.util.arrays.base;
import java.util.arrays.sorted;
class Some<T>{
T[] elements;
T[] getGreater(T element, Comparator<? super T> comparator){
T[] ret = elements.clone();
ret..sort( comparator );
int index =ret..binarySearch( element, comparator );
if (index==0) return ret;
if (index>=0) return ret..copyOfRange( index, ret.length );
else{
index = Math.min(-index -1, ret.length);
return ret..copyOfRange( index, ret.length );
}
}
}
When we write: ret..sort(...) java.util.arrays.base is added to import.
We can even make it more precise and write ret..base.sort(...) which
can help to be clear what logic we are using, or
ret..java.util.arrays.base.sort(...) not as nice as read but it allows
to write correct code in some rare cases.
In my opinion, this looks really simple and what's more, it's highly
resistant to changes. If someone adds the same method in some jar used
in project, a whole code will be still more or less OK. If we do not
have class with added method (let it be sort()) and we use short
notation ..sort(...), everything will be still OK (then we may want to
see the warning because that code is unclear) and if we used normal
notation ..base.sort(...), then (even if we have modified class in
import), all will be working fine. The only problem is when we have
two glue classes in import with the same methods and we use short
notation, when error should occurs.
All those behaviours are common for Java classes.
Now, the same code today, for comparing:
class Some<T> {
T[] elements;
T[] getGreater(T element, Comparator<? super T> comparator) {
T[] ret = elements.clone();
Arrays.sort(ret, comparator);
int index = Arrays.binarySearch(ret, element, comparator);
if (index == 0) return ret;
if (index >= 0) return Arrays.copyOfRange(ret, index, ret.length);
else {
index = Math.min(-index - 1, ret.length);
return Arrays.copyOfRange(ret, index, ret.length);
}
}
}
2. Glue classes localization:
As you can see glue class header contains in itself a method header
and can be used to determine if object is valid for binding:
class sorted <T> glue (T[]) {...}
So you can think about it as a class with method glue:
<T> glue (T[] valid){ /* nothing */ }
if type is valid for parameter 'valid' in this method, then glue will work.
This means that we can use intersection and bounds if we need them.
3. Glue class names.
Glue class names should start with lower case, because it groups
sub-logic rather than a new one.
4. header resolving of dynamic methods
Let's consider:
class sorted <T> glue (T[]) {
glue int binarySearch(this, T element,Comparator<? Super T>
comparator){...}
}
after mixing glue class header with binarySearch method we will obtain:
static <T> int binarySearch(T[] this, T element,Comparator<? Super
T> comparator){...}
So as you can see, finally it's s normal static method (with object
passed as first parameter in background) in which form glue method(s)
should be compiled (this would allow to call this methods from older
Java versions).
This means also that current utils classes can be mostly converted to
new form with backward compatibility.
--
Pozdrowionka. / Regards.
Lasu aka Marek Kozieł
http://lasu2string.blogspot.com/
More information about the coin-dev
mailing list