Function pointers

Community Forums/Monkey Talk/Function pointers

matibee(Posted 2011) [#1]
(I'm playing with the demo so can't post on the actual monkey forums yet, and the documentation is pretty sparse)

What's the format for a function pointer? In BMax it's:

Global func(x:Int,y:Int)

Function RoutineA( x:Int, y:Int )
	Print x + ", " + y
End Function 

func = RoutineA
func( 22, 33 )


Cheers


Canardian(Posted 2011) [#2]
Monkey uses class downcasts instead of function pointers.
For example:
Local instanceA:ClassNameA=ClassNameA(instanceB)
Monkey is a bit like Eiffel or Oberon-2, a very clean OOP language, but much less typing :)

Last edited 2011


Gabriel(Posted 2011) [#3]
I know I'm going to regret asking, but in what way does downcasting a class instance replace a function pointer?


Canardian(Posted 2011) [#4]
Well, you get the methods and attributes of another class to an instance which doesn't have them. I think it's useful when you have a central method or attribute accessor, which needs to run through all your different classes. So it's similar to a function pointer.

Last edited 2011


Gabriel(Posted 2011) [#5]
Well, you get the methods and attributes of another class to an instance which doesn't have them.

Yep, regretting it already. In most languages, you can only access methods and attributes which an instance *does* have by downcasting. All you're doing is unhiding the methods and attributes which were hidden by passing it as a base class. Are you saying that Monkey dynamically reallocates memory to instances at runtime to add in methods and attributes which were never present?


Canardian(Posted 2011) [#6]
I think it's still similar to function pointers, because there you also have a lowest common denominator, like the parameters to the function.

Last edited 2011


matibee(Posted 2011) [#7]
Ok, thanks.

I've found that since Monkey supports polymorphism, I don't actually need function pointers at all :)


Chroma(Posted 2011) [#8]
I know I'm going to regret asking, but what is polymorphism? And in what way does it replace a function pointer?


Canardian(Posted 2011) [#9]
Polymorphism means that the same class can use different types of attributes while the methods stay the same. In Monkey, this is done via the generic class (search in help -> language for generic).


Difference(Posted 2011) [#10]
@Lumooja: You are really mixing up concepts in creative - but in confusing ways :-)

It is a rather far stretch to call Function pointers and downcasting related.

Also, Polymorphism, which attempts to hide differences in implementation, and generics, which attemtps to highlight differences are not really the same thing.

Generics in Monkey can be used for very usefull things such as making a list of a certain class of objects like this:
Local dots:List<Dot> = New List<Dot>

Here is an example of using inheritance (Keyword Extends) to create Polymorphism

http://blitzmax.com/Community/posts.php?topic=78860


D4NM4N(Posted 2011) [#11]
Polymorphism basically means you can cast in and out of a derived type(s) and use a basetype to declare instances of a parent type. You can also use the properties of said type when cast as such.. etc..

....Here is a better description :D
In strongly typed languages, polymorphism usually means that type A somehow derives from type B, or type C implements an interface that represents type B. In weakly typed languages types are implicitly polymorphic

http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming

Last edited 2011


Yasha(Posted 2011) [#12]
...neither polymorphism nor generics have anything to do with function pointers, unless Monkey is using radically different definitions of those words from every other language.

Polymorphism is a core OO concept (i.e. if you aren't using it, you aren't writing OO code), that already existed in BlitzMax, so it's a bit disturbing to see it cause such confusion.

Last edited 2011


TartanTangerine (was Indiepath)(Posted 2011) [#13]
So example?

Class A
 Function B
  Print "stiff"
 End
End

I want to pass A.B() by reference to another function as a callback.
How?

Last edited 2011


Yasha(Posted 2011) [#14]
You don't. Monkey doesn't support function pointers or first-class function values.

If you really need to do something like this, you'll have to find a way to do it with helper classes (e.g. make the "callback" a method of a class that exists solely for the purpose of passing that method around, attached to an instance).

Class Callback Abstract
    Function proc() Abstract
End

Class Func1 Extends Callback
    Function proc()
        ...
    End
End

Class Func2 Extends Callback
    Function proc()
        ...
    End
End


In the above example, variables of type Callback can hold objects of type Func1 or Func2, which are invoked by calling the proc() method (if you make it an instance method you can have a clunky fake of functional programming in this way).

Last edited 2011


TartanTangerine (was Indiepath)(Posted 2011) [#15]
Yeah already doing it that way but since the topic appeared to be about function pointers I thought monkey might support them in some hidden way.


MGE(Posted 2011) [#16]
bummer....my entire engine is based off function pointers. hmm...


Vlad(Posted 2011) [#17]
Function and method pointers is required to work with events, callbacks and something like. This stuff is have to be in language!
And if target is flash then AS3 supports function pointers with special type :Function, so this things must have to be.


Yasha(Posted 2011) [#18]
1) ActionScript doesn't have function pointers, it has first-class functions (i.e. closures). Not the same thing at all, and will probably break your code if you try to use one as the other. (Closures are better, though.)

2) Why does their presence in ActionScript mean that they need to be available in Monkey? Monkey also has to be compatible with Java, which doesn't support either of the above concepts and works perfectly well (at least if you like Java) with events and UI code.

Quite to the contrary of the above post, Monkey can't support function pointers because it can't support any kind of pointer - the very-high-level targets (JavaScript and ActionScript) don't expose that level of the machine, while Java doesn't allow it and C# limits it greatly. It would immediately make for code that can't even be ported between compiler targets, let alone operating systems or other platforms, as everyone handles pointers differently. They're left out by intention, to try to make core language code compile on every target.

Last edited 2011


Vlad(Posted 2011) [#19]
OMG Yasha,
I'm use AS3 several years and you talking me it haven't function pointers? LOL man.
Read these first: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/Function.html

Why to use pointers? Because it's convenient! Because this is nice!

BTW: C++ have method and function pointers, VB.NET n' C# have delegates, Object Pascal have method and function pointers, Javascript have these pointers, and i can continue ...

And I can emulate method pointers with pure OOP, but these use more code, than just something like
local pfn:int(int, string) = myObject.method


Last edited 2011


D4NM4N(Posted 2011) [#20]
because it can't support any kind of pointer
ALL programming languages support pointers it is kind of a fundamental requirement, it just depends what kind of pointers you are talking about. Even simple array indexes and object instance identifiers are by -definition- pointers.

As for function pointers i have never really bothered with them (although probably used them without knowing it to be honest)
Do C# delegates fall under this? If so then yeah they are handy but I would not really miss them.

Last edited 2011


Yasha(Posted 2011) [#21]
I'm use AS3 several years and you talking me it haven't function pointers?


Yes. Yes I am. That article that you linked to does not describe function pointers. It describes first-class functions. They're not the same thing and they're not compatible.

ALL programming languages support pointers


Sorry. I meant explicit, C-style (and BlitzMax-style) pointers, not type-safe references to language objects which are generally quite a different thing to use. Obviously Monkey does support that kind of pointer.

The term "function pointer" is normally only used to refer to C-style pointers though, because it's extremely misleading when used in the context of function objects in a highly dynamic language (it doesn't refer to a simple section of code, it refers to an object that happens to support callable syntax).


MGE(Posted 2011) [#22]
In as3 I can do this:

Public var a:Function;

a = AFunctionSomeWhere;

// Call the function

a();

How would this be done in Monkey? Thx.


Vlad(Posted 2011) [#23]
@Yasha, OK my mistake in terminology. AS3 have function types instead of function pointers, but it works same as function pointers! In my fist post i wrote this. To call any method or function you should to have pointer to it, so function types contain pointer to method or function. So no big difference for me.


Beaker(Posted 2011) [#24]
Something like this:


Yasha is correct here.


Vlad(Posted 2011) [#25]
@Beaker, this is uncomfortable and generates a lot of redundant code.
Complete sample (in monkey) is:
Class DelegatePrint
	Field _mdl:MyClass

	Method New( t:MyClass )
		_mdl = t
	End
	
	Method Call()
		Print(">>> "+_mdl.fld)
	End
End

And if we need to call 10 or more methods of object? This add 6 additional strings per each call and more longest code (new DelegatePrint(m) instead of m.print)

Last edited 2011


Xaron(Posted 2011) [#26]
I think you have to do it the Java way. Using interfaces. So yes, this is some kind of overhead.


Yasha(Posted 2011) [#27]
it works same as function pointers! In my fist post i wrote this. To call any method or function you should to have pointer to it, so function types contain pointer to method or function. So no big difference for me.


Actually your code in post #25 highlights precisely what the difference is:
Method New( t:MyClass )
	_mdl = t
End

Since function pointers (as used in C, C++ and BlitzMax) point to a block of executable code, they can't store values, so a true function pointer wouldn't do what you want. "Functions" (as function objects) in more dynamic languages, like ActionScript, are objects that contain both a C-style code pointer and a data object holding scoped variables (this is a massive oversimplification), so 1) they can hold values and 2) unlike with static variables (inner globals) those values can be different between two objects that invoke the same code block. The "Java way"described above is doing something similar to a true-function in dynamic languages, but making the hidden stuff explicit.

Anyway I can think of three and a half reasons why first-class functions aren't in Monkey:
- not supported by C++ or Java, which only have plain function pointers/objects respectively (although tweaks to the compiler could overcome this without real difficulties)
- would break semantic compatibility with BlitzMax, where inner functions didn't capture state
- one of the Monkey goals appears to have been to make the language simpler. Adding stateful functions to a language originally designed without them would probably confuse many users

I'm sorry to ramble on... but I hope this helps clarify the differences between the two competing features, and why neither exists.

Last edited 2011