semi-OT: is indentation based scoping off-putting

79 views
Skip to first unread message

david tweed

unread,
Aug 2, 2012, 3:59:04 PM8/2/12
to juli...@googlegroups.com
OK, let me state this LOUD: I'm NOT proposing that Julia start using Python style indentation for scoping.

However, I'm thinking about embedding a DSL language in Julia/making it easy to interop via LLVM with such a language. One of the issues is that it's trying to acheive Matlab / APL / Q/kdb+ "conciseness", but being slightly more explicit about some things. There's a need to indicate the scope of certain "implicit" variables. Two traditional options

reduce
    stmt
    stmt
    where
         stmt
         stmt
         reduce
             stmt
         end
     end
end

(where indiation is human-user only, no significance to program) or
reduce:
    stmt
    stmt
    where:
         stmt
         stmt
         reduce:
             stmt

The second one has three less lines, but whenever you mention Python to some mainstream programmers some sizable number go into berserker mode. I'm just wondering what the kind of people intersted in Julia think: would an indentation based syntax be significantly offputting, does the verbosity of explicit ends annoy you, or no real opinion?

Many thanks for any thoughts

Jeffrey Sarnoff

unread,
Aug 2, 2012, 4:47:32 PM8/2/12
to juli...@googlegroups.com
Using explicit end(s) matches Julia's approach, a consideration and a genteel way of avoiding writing my true feelings about the years of hassle that Python's aura of indentation has brought to my fingertips.  Indentation semantics is a good idea in the abstract.  The way to go with indentation semantics to count a '\t' as 1 space and to warn (as an option) if indentation is encountered that mixes use of spaces and tabs.

Stefan Karpinski

unread,
Aug 2, 2012, 5:01:07 PM8/2/12
to juli...@googlegroups.com
I find indentation-based blocks to be fiddly and annoying quite often (especially when cutting and pasting code, which is a pretty common occurrence). I also find it harder to read than having an explicit block closing marker on its own line. But that's just, like, my opinion, man. Maybe I'm biased from being more of a C and Ruby programmer than a Python programmer.

One thing that does really annoy me is having to write one-liners like this:

for k=1:10 println(k) end

The fact that Julia's parser lets you *not* sprinkle the line with semicolons (or commas), is liberating, but I'd love to leave the end out altogether. A possible syntax rule that would allow this is: if the loop body begins on the same line as the loop preamble, then the entire loop must fit on a single line and the end is optional. That would preserve backwards compatibility with all but the most bizarrely formatted loops, yet allow trailing ends to be omitted in one-liners. The same rule could apply with if-else and such. E.g.

if     k < n println("less")
elseif k > n println("more")
else         println("same")

That's rather pleasant and readable, imo. (I wish that Python would allow one-line conditionals and loops too.)

--
 
 
 

Jeffrey Sarnoff

unread,
Aug 2, 2012, 5:07:26 PM8/2/12
to juli...@googlegroups.com
+1 what Stefan said

There is one other stress inducing aspect to Python's use of indentation, the stacked sections of nested indentation want the same indentation for everything at a given level of nesting .. forcing a layout that is often inartfully spaced somewhere.  If your semantic indentation understands that a white space appears to be white space [not spaces, and vice versa] and is good with

reduce:
      stmt
      stmt
      where:
         stmt
         stmt
      reduce:
          stmt
          stmt
      where:
        stmt
        stmt

It would be a step forward from ALGOL
         
           
reduce:
    stmt
    stmt
    where:
         stmt
         stmt
         reduce:
             stmt

John Cowan

unread,
Aug 2, 2012, 5:10:05 PM8/2/12
to juli...@googlegroups.com
On Thu, Aug 2, 2012 at 5:01 PM, Stefan Karpinski <ste...@karpinski.org> wrote:

> (I wish that Python would allow
> one-line conditionals and loops too.)

Actually, it does; you can write

if (a == b): print "same"

though at the REPL this must be followed by a blank line.
This isn't considered particularly good Python style, AFAIK.

--
GMail doesn't have rotating .sigs, but you can see mine at
http://www.ccil.org/~cowan/signatures

Stefan Karpinski

unread,
Aug 2, 2012, 5:12:49 PM8/2/12
to juli...@googlegroups.com
On Thu, Aug 2, 2012 at 5:10 PM, John Cowan <johnw...@gmail.com> wrote:

> (I wish that Python would allow
> one-line conditionals and loops too.)

Actually, it does; you can write

if (a == b): print "same"

though at the REPL this must be followed by a blank line.
This isn't considered particularly good Python style, AFAIK.

Well waddya know. I was not aware of that.
Reply all
Reply to author
Forward
0 new messages