Default/Optional/Generated interface methods again

Monkey Forums/Monkey Programming/Default/Optional/Generated interface methods again

Samah(Posted 2015) [#1]
Mark,
http://www.monkey-x.com/Community/posts.php?topic=8215&post=83616
Almost a year ago we had this discussion, and your response was essentially "I'll think about it".
Have you looked at it?

My other suggestion that would satisfy everyone is to have a keyword that generates empty methods for you. There is no change to interfaces, so all methods are still mandatory. The difference is that you can put an empty method in with a single line of code rather than implementing the whole thing.

Interface Foo
	Method One:Void()
	Method Two:Void()
	Method Three:Int()
End

Class Bar Implements Foo
	' explicitly implementing One
	Method One:Void()
		' stuff
	End

	' implicitly generating Two and Three
	Method Two:Void() Generate
	Method Three:Int() Generate
End


Gives you:
Class Bar Implements Foo
	Method One:Void()
		' stuff
	End
	
	Method Two:Void()
		' nothing
	End
	
	Method Three:Int()
		' return default value for the type
		Return 0
	End
End



Nobuyuki(Posted 2015) [#2]
neat idea, might save some time, but .....

if the interfaces you're using are getting complicated enough to make you run through the inconvenience of returning a lot of default / unused methods in the classes that implement them, then shouldn't the interface be split up? "god" interfaces seem like as much an anti-pattern as "god classes" would be if they contained a lot of abstract methods...

(Edit: says the guy who wishes Monkey had auto-implemented properties or Jungle supported automatically creating them from a var and folding the getter/setter down to a single line...)


Samah(Posted 2015) [#3]
@Nobuyuki: ...then shouldn't the interface be split up?

I know what you mean, but there are many situations where it would be incredibly useful. Listener/event handler patterns are of course the perfect example.
Interface MouseListener
	Method MouseDown:Void(button:Int, x:Int, y:Int)
	Method MouseUp:Void(button:Int, x:Int, y:Int)
	Method MouseMove:Void(x:Int, y:Int)
End

Class MyHandler Implements MouseListener
	' we care about mouse move events
	Method MouseMove:Void(x:Int, y:Int)
		Print "Mouse at: " + x + "," + y
	End
	
	' don't care about clicks
	Method MouseDown:Void(button:Int, x:Int, y:Int) Generate
	Method MouseUp:Void(button:Int, x:Int, y:Int) Generate
End

handlers.AddMouseListener(New MyHandler)


@Nobuyuki: (Edit: says the guy who wishes Monkey had auto-implemented properties or Jungle supported automatically creating them from a var and folding the getter/setter down to a single line...)

Exactly. I've had this thought too since forever... :(