Forums

Suggestion: Forward Methods to Object Variables

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Suggestion: Forward Methods to Object Variables

Postby oahmad04 » Fri Jan 03, 2014 1:44 am

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.
oahmad04
 
Posts: 19

Re: Suggestion: Forward Methods to Object Variables

Postby nerdzero » Fri Jan 03, 2014 5:16 pm

Yeah, I want something like this. This is similar to the decorator idea Charles had a while back: viewtopic.php?f=4&t=1038
nerdzero
 
Posts: 286
Location: Chicago, IL

Re: Suggestion: Forward Methods to Object Variables

Postby oahmad04 » Sat Jan 04, 2014 4:56 pm

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?
oahmad04
 
Posts: 19


Return to Discussion

Who is online

Users browsing this forum: No registered users and 81 guests

cron