Page 1 of 1

Suggestion: Forward Methods to Object Variables

PostPosted: Fri Jan 03, 2014 1:44 am
by oahmad04
I have an unusual suggestion. First, let me outline the problem I am trying to solve. Imagine you are making a custom class for WX.Net. This class is a labeled text entry box. It inherits wx.Panel and has the object variables .label (wx.StaticText object) and .entry (wx.TextCtrl object). If you want to support many of the methods TextCtrl supports you have to do the following:
def getValue as String
"""Returns text in entry box"""

return .entry.getValue

def setValue(text as String)
"""Sets the text in the entry box"""

.entry.setValue(text)

That's fine for two methods. It gets annoying for 30. I propose the following syntax:
Code: Select all
forward to .entry
    getValue
    setValue

forward to .label
    getLabel
    setLabel

The above is the equivalent of:
def getValue as String
"""Docstring imported from wx.TextCtrl method"""

return .entry.getValue

def setValue(text as String)
"""Docstring"""

.entry.setValue(text)

def getLabel as String
"""Docstring"""

return .label.getLabel

def setLabel(text as String)
"""Docstring"""

.label.setLabel(text)

This makes code much shorter, and generates docstrings from TextCtrl and StaticText (the programmer may make a mistake in the docstring, or may be too lazy to write docstrings for all the borrowed methods)

Thoughts?

Wow, trying to post this on a smartphone was annoying.

Re: Suggestion: Forward Methods to Object Variables

PostPosted: Fri Jan 03, 2014 5:16 pm
by nerdzero
Yeah, I want something like this. This is similar to the decorator idea Charles had a while back: viewtopic.php?f=4&t=1038

Re: Suggestion: Forward Methods to Object Variables

PostPosted: Sat Jan 04, 2014 4:56 pm
by oahmad04
nerdzero wrote:Yeah, I want something like this. This is similar to the decorator idea Charles had a while back: viewtopic.php?f=4&t=1038

Ooh, I like this. That way you don't even have to name the methods, but it's less flexible. Going back to my labeled entry example, what if you don't want to support methods that are only useful if the TextCtrl object is being used as a multiline text editor? What about methods TextCtrl shares with Panel? Would you want to override these methods? What about methods shared between StaticText and TextCtrl?