I'm not sure the block syntax buys as much as you'd want it to. Maybe a more complex example would help?
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:
Trying again: With this, it would be possible for a user to create a switch/case statement of their own.
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.
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.