Interface inconsistency? Allow "Function" ?

Monkey Forums/Monkey Programming/Interface inconsistency? Allow "Function" ?

Difference(Posted 2012) [#1]
I was trying to declare a function as part of an interface, but found out that Monkey doesn't allow it.

I can however declare my function as a method, but implement it as a function.

Should Monkey be changed to allow "Function" in interfaces ? I'd like that.
Or should it alternatively complain about the function implementation?

Strict

Interface MyInterface
	Method MyMethod:Int(x:Int)	
	Method MyFunction:Int(x:Int)	' Function MyFunction:Int(x:Int) not allowed

End Interface

Class MyClass Implements MyInterface

	Method MyMethod:Int(x:Int)		
		Return x * 2
	End Method
		
	Function MyFunction:Int(x:Int)	
		Return x * 3
	End Function

End Class


Function Main:Int()

	Local foo:= New MyClass

	Print foo.MyMethod(1)
	Print foo.MyFunction(2)
	
	Return 0
End Function



ziggy(Posted 2012) [#2]
From my point of view, I think it is ok as it is now. In my honest opinion, any shared member like a function should have a single implementation, and that makes it very weird inside an interface. It has no sense to force any class of a given kind (interface) to implement its own shared member of this interface. If you need this, you need a method. If you don't, you don't need this inside the interface.


Samah(Posted 2012) [#3]
Having a function in an interface makes no sense. One of the main points of interfaces is to allow you to call methods on any object that implements them, without having to know what kind of class that object is. In that situation, you have an object. If you were to call a function, you need to know what the class is, which defeats the purpose of using an interface.


ziggy(Posted 2012) [#4]
I was trying to explain exactly what Samah has explained in a much better way.


Difference(Posted 2012) [#5]
Ok, I get the point. I think was trying to force some classes to implement an Init() Function without extending a particular class.

The below version (where foo is declared as MyInterface) actually complains that MyFunction is not implemented, so I guess Monkey catches the Function/Method mixup when it makes sense.

Strict

Interface MyInterface
	Method MyMethod:Int(x:Int)	
	Method MyFunction:Int(x:Int)	

End Interface

Class MyClass Implements MyInterface

	Method MyMethod:Int(x:Int)		
		Return x * 2
	End Method
		
	Function MyFunction:Int(x:Int)	
		Return x * 3
	End Function

End Class


Function Main:Int()

	Local foo:MyInterface = New MyClass

	Print foo.MyMethod(1)
	Print foo.MyFunction(2)
	
	Return 0
End Function