Any Python users willing to some feedback on some language ideas I had? Thanks
gist.github.com
gist.github.com
For instance, you can create similar syntax with a class definition. I've tossed together something quick below to emulate your UI library (not really something I've tried in practice, but the idea is there):
class ExampleLabel:
caption = "This is an example"
position = (50, 50)
class ClickMeButton:
caption = "Click me! Click me!"
on_click = lambda: sys.stdout.write("The button was clicked.\n")
class HelloWorldWindow:
caption = "Hello World"
dimensions = (400, 300)
contains = (ExampleLabel, ClickMeButton)
If you then had classes or functions with kwargs arguments, that recognized key names such as "caption", "on_click", etc., then the above classes could be used in this way to instantiate things (either alone, or within something like a Window): b = Button(**ClickMeButton.__dict__)
l = Label(**ExampleLabel.__dict__)
...or equivalently, on the fly: l = Label(caption="Other label", position=(10, 20))
Notice that since everything is defined as a class, instantiating duplicates is actually taken care of. For example: w1 = Window(**HelloWorldWindow.__dict__)
w2 = Window(**HelloWorldWindow.__dict__)
Since I haven't "inlined" anything in a block assignment, the definition can be used repeatedly (when put in a class).I tried to keep the examples simple so that the functionality was clear, I'll try to come up with a more concrete example to demonstrate it's utility.
I'm aware that you can achieve a a lot of already using classes (and metaclasses), but I thought a clearer syntax would be helpful. I don't seeing my code cluttered with "class" when that's not what's actually happening, but of course that could be exactly what was happening in my example.
One thing I originally had in my example (though I decided it wasn't a good idea so I removed it) was
ui.on_event "click", "altclick":
print("I've been clicked!")
where on_event macro takes the paren-less tuple of event names and returns another macro which takes the block, then does the correct assignment.I realize this also isn't a strong example, but it's a little bit more than I have in the post.
n = 3
switch n:
case 1:
print("n is one")
case 2:
print("n is two")
default:
print("n is neither one or two")
by injecting "case" into the namespace of a block in "switch", we'd only have to add one identifier to the global namespace. Here's what some of the implementation code might look like. @macro
def switch(value):
@macro
def _switch(block):
cases = dict()
def line_callback(value):
if isinstance(value, SwitchCase):
cases[SwitchCase.value] = SwithCase.block
elif isinstance(value, DefaultCase):
default = DefaultCase.block
block(line_callback=line_callback)
try:
result_block = cases[value]
except KeyError:
result_block = default
return(result_block())
return(_switch)
I wasn't thinking of it exclusively as a data structure tool, but also possibly as a way to create your own flow control operations and such.This might not be the most compelling possible example in the world, but I hope it helps.