purpose of the end keyword

763 views
Skip to first unread message

Dustin Lee

unread,
May 15, 2014, 2:51:57 PM5/15/14
to julia...@googlegroups.com
Coming from python I've found that julias's "end" statement doesn't bother me as much as I would have thought, but when I show some code to my python colleagues this really annoys them.  But this got me thinking, what *is* the purpose of "end".  Is it just a taste issue, a way to make parsing easier, something else?

My make believe answer that I found myself starting to make to my colleagues was that it was for ease of writing a parser, but then I wasn't sure how this squares with the fact that languages like python and haskell don't seem to have too much trouble w/out braces or end keywords.

Just curious,

dustin

Stefan Karpinski

unread,
May 15, 2014, 2:55:07 PM5/15/14
to Julia Users
The `end` keyword closes blocks. Python uses indentation for this. In Julia indentation is not significant.

Dustin Lee

unread,
May 15, 2014, 3:04:41 PM5/15/14
to julia...@googlegroups.com
Thanks.  I should probably have been more clear.  I understand what it *is* doing.  I'm just curious if there was a reason besides "taste" to choose that over whitespace significance.

Stefan Karpinski

unread,
May 15, 2014, 3:14:19 PM5/15/14
to Julia Users
It's a matter of taste – and the fact that we wanted Julia to feel familiar in particular to Matlab users (and to a lesser extent Ruby users). I personally don't like significant indentation. It gets really awkward and fiddly when you're trying to cut and paste into a terminal or into an editor. I've seen a significant number of live Python demos flounder as the presenter struggled with indentation issues. It feels to me like Python programs trail off into space with never-ending scopes. Jeff and Viral both happen to feel similarly, so Julia ended up looking more like Matlab than like Python.

Steven G. Johnson

unread,
May 15, 2014, 4:03:10 PM5/15/14
to julia...@googlegroups.com
It's a tradeoff. The cost of having an explicit "end" is a few extra characters that you have to type, and an extra line of code at the end of each block.  On the other hand, the benefits include less error-prone cut-and-paste (as Stefan mentioned), the possibility of automated reformatting of code (e.g. think emacs indent-region or gofmt), and flexibility in writing one-liners (e.g. "try foo() end").

I think the metaprogramming facilities in Julia also favor the choice of explicit block delimiters.  In Julia, code can also be a symbolic expression, simply by surrounding it with :(....) or quote ... end, and whitespace sensitivity within symbolic expressions seems like it would get annoying quickly

Probably this should be in the Julia FAQ or Manual, since our inexplicable rejection of the holy whitespace seems to be the first thing that every Python programmer asks about.  (If you hate typing extraneous characters, the colons in Python must drive you bonkers.)

(People who like maximum terseness in a practical programming language should take a look at J.   The J examples on RosettaCode are pretty amazing.)

Stefan Karpinski

unread,
May 15, 2014, 4:12:47 PM5/15/14
to Julia Users
Yes, the point about metaprogramming is a good one.

James Porter

unread,
May 18, 2014, 10:56:20 PM5/18/14
to julia...@googlegroups.com
The really compelling thing about a non-whitespace block ending delimiter for me is editors being able to automatically indent my code. I've been bitten by far too many python bugs that Emacs would have caught for me in a second if python had and `end` keyword instead of syntactic whitespace.
Reply all
Reply to author
Forward
0 new messages