Implicit main()?
Posted: Thu Jan 09, 2014 6:41 pm
Hey all,
Thank you, Charles, for Cobra. I'm really getting into it as a tolerably fun-to-code compiled language, and I'm happy to have something lower level than Python but less aggravating than C<whatever>.
One thing that annoys me about C++/C#/Java though, which has carried by inheritance into Cobra it seems, is the requirement for a class and method to contain statements that should run at program launch. That is, after all the classes are made, the statements that actually use those classes and methods to do stuff are always wrapped in a class and method.
I won't ask why anyone thought this was a good idea to begin with, because to me it just looks like so much cruft. I'm sure they had their reasons. Instead, I'll just make a little code request.
As a proud Pythonista, I'd be happy to see a way to directly code without explicit classes, and have the compiler wrap those bottom-level statements in a class and method automatically. So if a code fragment contains function definitions and statements outside of a class, the functions are silently converted to methods of an anonymous wrapper class and the statements are placed into a main() method of that class.
I realise this isn't as dynamic as Python and so there would still be constraints on how to code in this way so that Cobra doesn't throw a fit. For example in Python one can write a function, use it several times, then overwrite it, or conditionally overwrite it, or wrap it: I'm not sure this sort of dynamicism is possible in Cobra, and so Cobra might throw a bug, but if that bug is thrown after rewriting the code into an implicit class/method, then it won't be obvious to the hapless coder where they made their mistake.
Still, it's food for thought; less cruft, more aesthetically pleasing code, but it's implicit conversion so purists can still code in
if they so choose.
Thank you, Charles, for Cobra. I'm really getting into it as a tolerably fun-to-code compiled language, and I'm happy to have something lower level than Python but less aggravating than C<whatever>.
One thing that annoys me about C++/C#/Java though, which has carried by inheritance into Cobra it seems, is the requirement for a class and method to contain statements that should run at program launch. That is, after all the classes are made, the statements that actually use those classes and methods to do stuff are always wrapped in a class and method.
I won't ask why anyone thought this was a good idea to begin with, because to me it just looks like so much cruft. I'm sure they had their reasons. Instead, I'll just make a little code request.
As a proud Pythonista, I'd be happy to see a way to directly code without explicit classes, and have the compiler wrap those bottom-level statements in a class and method automatically. So if a code fragment contains function definitions and statements outside of a class, the functions are silently converted to methods of an anonymous wrapper class and the statements are placed into a main() method of that class.
I realise this isn't as dynamic as Python and so there would still be constraints on how to code in this way so that Cobra doesn't throw a fit. For example in Python one can write a function, use it several times, then overwrite it, or conditionally overwrite it, or wrap it: I'm not sure this sort of dynamicism is possible in Cobra, and so Cobra might throw a bug, but if that bug is thrown after rewriting the code into an implicit class/method, then it won't be obvious to the hapless coder where they made their mistake.
Still, it's food for thought; less cruft, more aesthetically pleasing code, but it's implicit conversion so purists can still code in
class Prog
cue main()
print "Feel better about myself"
if they so choose.