Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > simple method to simulate function pointers in java

Reply
Thread Tools

simple method to simulate function pointers in java

 
 
Lino Ferrentino
Guest
Posts: n/a
 
      09-02-2011
Maybe there are other methods... I use this:

public final class b_callbacks {

private b_callbacks(){
}


public static interface void_f_void{
public void f();
}

public static interface void_f_int{
public void f(int i);
}


}

----

when I want to use a function pointer

import b_callbacks.void_f_int;

private void_f_int my_callback;

my_callback = new void_f_int(){
public void f(int i){
//code
}
};

to call the callback

my_callback.f(42);


====

For each function pointer type we create an interface.
 
Reply With Quote
 
 
 
 
Mayeul
Guest
Posts: n/a
 
      09-02-2011
On 02/09/2011 10:54, Lino Ferrentino wrote:
> Maybe there are other methods... I use this:
>
> [Functor interfaces]


Well yes, that's pretty much it.
Note you do not have to declare these interfaces as nested in a class.
You may, if you think it makes matters clearer, but you don't have to.

public interface PettingStrategy {

void pet(Dog dog);

}

Note that it will usually lead to unclear terminology when you think in
terms of 'function pointers'. Function pointers are a general-purpose
language mechanism, which happen to not exist in Java. No need to pursue
it to the end.
Whenever you have a need for a mechanism of the sort, there is a
precise, well-defined, task you need to perform with this mechanism. You
should create interfaces designed to perform this task, and name these
interfaces and their methods accordingly. This will make your programs
clearer.

--
Mayeul
 
Reply With Quote
 
 
 
 
Lino Ferrentino
Guest
Posts: n/a
 
      09-02-2011
>
> It will also make your code look and read more like idiomatic Java and
> less like a literal translation of some other language.


ok... you are right. I was simply try to reduce Java to c.

So I needed function pointers, typedefs... and functions (not methods)


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-02-2011
On Fri, 2 Sep 2011 01:54:45 -0700 (PDT), Lino Ferrentino
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

see http://mindprod.com/jgloss/delegate.html

It is a verbose, somewhat more powerful technique than function
pointers.
--
Roedy Green Canadian Mind Products
http://mindprod.com
The modern conservative is engaged in one of man's oldest exercises in moral philosophy; that is,
the search for a superior moral justification for selfishness.
~ John Kenneth Galbraith (born: 1908-10-15 died: 2006-04-29 at age: 97)
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      09-06-2011
On 9/2/2011 8:11 AM, Lino Ferrentino wrote:
>>
>> It will also make your code look and read more like idiomatic Java and
>> less like a literal translation of some other language.

>
> ok... you are right. I was simply try to reduce Java to c.
>
> So I needed function pointers, typedefs... and functions (not methods)


Trying to program in language X using the paradigms of
language Y usually leads to bad code.

If you want to use language X then learn to do it the
X way.

Arne



 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      09-06-2011
On 9/2/2011 4:54 AM, Lino Ferrentino wrote:
> Maybe there are other methods... I use this:
>
> public final class b_callbacks {
>
> private b_callbacks(){
> }
>
>
> public static interface void_f_void{
> public void f();
> }
>
> public static interface void_f_int{
> public void f(int i);
> }
>
>
> }
>
> ----
>
> when I want to use a function pointer
>
> import b_callbacks.void_f_int;
>
> private void_f_int my_callback;
>
> my_callback = new void_f_int(){
> public void f(int i){
> //code
> }
> };
>
> to call the callback
>
> my_callback.f(42);
>
>
> ====
>
> For each function pointer type we create an interface.


It is perfectly valid.

You need to note two things:
1) the codes is not following Java coding convention
2) it is based on an assumption that all functions with
the same signature are interchangeable - that is not
very type safe

Arne

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      09-06-2011
Arne Vajh°j wrote:
> Lino Ferrentino wrote:
>> Maybe there are other methods... I use this:
>>
>> public final class b_callbacks {
>>
>> private b_callbacks(){
>> }
>>
>>
>> public static interface void_f_void{
>> public void f();
>> }
>>
>> public static interface void_f_int{
>> public void f(int i);
>> }
>>
>>
>> }
>>
>> ----
>>
>> when I want to use a function pointer
>>
>> import b_callbacks.void_f_int;
>>
>> private void_f_int my_callback;
>>
>> my_callback = new void_f_int(){
>> public void f(int i){
>> //code
>> }
>> };
>>
>> to call the callback
>>
>> my_callback.f(42);
>>
>>
>> ====
>>
>> For each function pointer type we create an interface.

>
> It is perfectly valid.
>
> You need to note two things:
> 1) the codes is not following Java coding convention
> 2) it is based on an assumption that all functions with
> the same signature are interchangeable - that is not
> very type safe


The Java idiom is to use a functor, typically a single-abstract-method (SAM) interface, and pass an instance of that functor type to the object needing a callback or similar method. The object implementing the functor interface is the called-back object.

And as Arne says, do follow the coding conventions. Your code was nearly impenetrable. You name things by purpose in Java, not by implementation type.

Roughly (very roughly)_speaking, the functor pattern is something like:

public interface Updater
{
public void update( Information info );
}

============

public class SomethingThatUpdates
{
private final List <Updater> updaters = new ArrayList <> ();

public void addUpdater( Updater updater )
{
updaters.add( updater );
}

public void doUpdates()
{
Information info = obtainInformationSomehow();
for( Updater upd : updaters )
{
upd.update( info );
}
}

// etc.
}

============

public class SomethingToUpdate implements Updater
{
public void init( SomethingThatUpdates something )
{
something.addUpdater( this );
}

public void update( Information info )
{
// do something useful with 'info'
}

// etc.
}

============

--
Lew
 
Reply With Quote
 
John B. Matthews
Guest
Posts: n/a
 
      09-06-2011
In article
<(E-Mail Removed)
>,

Lew <(E-Mail Removed)> wrote:

> Arne Vajh├Şj wrote:
> > Lino Ferrentino wrote:
> >> Maybe there are other methods... I use this:
> >>
> >> [code omitted]
> >>
> >> to call the callback
> >>
> >> my_callback.f(42);
> >>
> >>
> >> For each function pointer type we create an interface.

> >
> > It is perfectly valid.
> >
> > You need to note two things:
> > 1) the codes is not following Java coding convention
> > 2) it is based on an assumption that all functions with
> > the same signature are interchangeable - that is not
> > very type safe

>
> The Java idiom is to use a functor, typically a
> single-abstract-method (SAM) interface, and pass an instance of that
> functor type to the object needing a callback or similar method. The
> object implementing the functor interface is the called-back object.
>
> And as Arne says, do follow the coding conventions. Your code was
> nearly impenetrable. You name things by purpose in Java, not by
> implementation type.
>
> Roughly (very roughly)_speaking, the functor pattern is something like:
>
> [code omitted]


A related example of this widely used pattern may be found in
`EventListenerList` [1], which adds a measure of type safety by
using a class literal as a runtime type-token [2].

[1]<http://download.oracle.com/javase/7/docs/api/javax/swing/event/EventListenerList.html>
[2]<http://download.oracle.com/javase/tutorial/extra/generics/literals.html>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PipeController v0.1 - experimental tool to simulate simple UNIX-stylepipes in Python vasudevram Python 3 09-02-2012 08:56 PM
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
void pointers & void function pointers Peter Goddard C Programming 3 05-16-2005 09:44 PM
Simulate post method in a servlet delph Java 3 05-13-2005 02:48 PM
Template specialization of pointers with function pointers Phil C++ 1 09-16-2003 02:17 AM



Advertisments