Arjun
Arjun

Reputation: 1711

Coding language customization

Is there any way to customize deep portions of a language to your taste? In Python I would like to use | or : to denote the boundary of a set. I hate typing set(some_list) or explicitly set([a,b,b,c,d]). I've been using s() by setting s = set but this wouldn't work on code others have to read.

Perhaps a "translator" text-editor add-on which would save 2 copies of your code, one with your own syntax and one with the standard syntax. Is there such a thing for Python?

Upvotes: 1

Views: 145

Answers (2)

SK-logic
SK-logic

Reputation: 9715

It is possible indeed to customise your language as you like. Some languages provide powerful metaprogramming features (unfortunately, Python is not one of them). Take a look at Lisp macros, or Nemerle, or Converge (the latter is the closest to Python). Some languages, like Katahdin, would even allow you to modify the very syntax easily.

But this sort of power should not be used for petty language modifications - it would hinder the readability and won't add any significant benefits. It's much better to use metaprogramming for implementing the embedded Domain Specific Languages, preferably with distinctive syntax, so it would be obvious for a reader when you're using the core language constructs and when it is a eDSL code.

Of course, if you're designing your own language from scratch or implementing an existing language, you can also use metaprogramming and syntax alteration tricks for the core language functionality as well, it is a much more flexible approach than having a huge, bloated core language from the very beginning.

Upvotes: 1

gfortune
gfortune

Reputation: 2619

Clarity and readability is a very important trait in production code. Remember that far more effort is generally expended into reading existing code than is spent writing new code. Maintenance is a serious consideration.

With that in mind, doing something like s = set and using s() instead of set() adds no functional benefit and is detrimental to readability while only offering the savings of a couple keystrokes. I think you recognize that on some level, but part of you isn't willing to accept the additional keystrokes in the name of readability.

I urge you to consider something like a macro instead so you feel better about pressing only a single key while still producing the same code. Something like a Logitech G15/G110/G19 keyboard even allows you to do this independent of the editor you're using so you don't need a software add-on. A few mice would allow you to do something similar as well (oh how I love my Razer Naga). Of course, a basic macro feature in software would work as well, but you're probably pressing a key combination to do it and just typing set( is probably faster anyway.

Upvotes: 6

Related Questions