r/ProgrammingLanguages • u/GoblinsGym • Jun 04 '26
semantic white space vs. blocks - maybe a middle ground ?
Working on my minimalist language.
I come from the Pascal / Delphi side, where begin / end means a good amount of typing and text soup. Modula-2 and Oberon add to the fun by forcing you to shout BEGIN .. END.
Some languages use endif or fi, but that clutters up the name space with more keywords, and you still get text soup.
I find { braces } a bit distracting. They are also a pain to type on many international keyboards.
Indentation is a visually clean way to demarcate blocks. Can be risky when doing cut and paste.
How about using a period to end a block, just as we end a sentence ? And let the compiler also check indentation as a safety ?
Example:
if condition_1
action1
if condition_2
action2 .
else
action3 .
# common continuationOne tradeoff is that you have to keep struct references together, cannot insert white space:
base.field # ok
base. field # ok, but ugly
base . field # won't workOpinions ? Looks too much like Cobol ? Belt and suspenders ?
Edit: Thank you all for the great input !
29
u/hrvbrs Jun 04 '26
Consider what makes good diff. Strict JSON is hated because of its lack of support for trailing commas. When you add a new item to the array, you have to put a comma on the last item and then insert the new item, making the diff dirty af. Same for deleting the last item. Using any kind of syntax to distinguish the last statement in a block (if put on the same line) is bound to run into the same problem.
TLDR, code is never rigid or permanent. It lives, grows, changes, and decays. Think about what PR reviewers will see when working in your language.
12
u/Athas Futhark Jun 04 '26
It used to be common to use the whole collection of punctuation characters: commas, semicolons and full stops. Nowadays I think it is only Erlang that maintains this tradition, but it's really not a bad way to do it. I would recommend using a semicolon to end a block however, and a full stop to end a definition; just like how we use semicolons within sentences.
The biggest problem with your design, however, is that it is not clear which block is ended by the terminator character. Why does the full stop after
action2end the outermostif(judging by the indentation of theelse) rather than the innermostif? This looks like an instance of the "dangling else" problem, which arises precisely when you don't use braces or a similar grouping mechanism.3
u/WalkerCodeRanger Azoth Language Jun 04 '26
The conflict with member access is a real problem. Also, in many languages, it would be hard to tell when the condition ends. So you'll need some kind of terminator on the condition unless it must be on one line (or use a line continuation character).
I've come up with something similar to your proposal. Many have argued that
--is a bad operator and people should just use-= 1. So imagine using that as a block end. In a language where the indent convention was two spaces this could work very well. Your example would become:They would be allowed to be combined to terminate multiple blocks. For example
The
----here is two operators and terminates both if statements2
u/rleim_a Jun 04 '26 edited Jun 04 '26
I'd keep the period on a separate line. Too easy to miss it's even there
What about indicating no
elseclause follows instead of marking the end of the block?another way is to have a distinct if-like keyword
nc for "no complement"
2
u/6502zx81 Jun 04 '26
Invisible characters that have semantics are confusing to beginners. They introduce many problems (paste, switching editors screws it up, ...). I do minimal highlighting: almost monochrome and have structure in bold (i.e. if, else, while, ( { ) } etc. other keywords are not highlighted.
2
u/flatfinger Jun 04 '26
Languages which treats line breaks as significant should provide a means of indicating line continuations. I find it somewhat ironic that the first high-level language to support line continuations did so by placing a mark on the following line, even though putting a marker on the preceding line might have saved a few bytes of RAM during the initial phase of compilation (though it's entirely possible that any non-contrived program that would have come close to overflowing memory at that stage would cause later stages to run out of RAM, making early-stage RAM usage irrelevant), but later languages have generally opted to mark the end of the preceding line. If I were designing a language, I think I would specify that marks must be placed at the start of every line of a multi-line statement. If international keyboards weren't an issue, I'd probably use:
The indent on the continuation lines would be recommended, but not semantically significant. One slight issue is that if indentation were generally semantically significant, I wouldn't know how the continuation characters should interact with it, but continuations would be visually obvious, and misplaced cut-and-paste operations that copy part of a multi-line statement would generally be flagged as erroneous.
29
u/binarycow Jun 04 '26
Disclaimer: I'm primarily a C# developer, so I'm coming from that perspective.
I know you said you don't like braces, but I urge you to reconsider.
While some say python has a "simple syntax" - and the colon and indentation seems simple - it's way more complex than it would appear. Indentation matters, except when inside of certain syntax elements, and then it doesn't. Copy/paste can become problematic. The whole tabs vs. spaces argument actually has consequences.
With C-inspired languages, the rules are simple.
{and}surround a sequence of zero or more statements, it creates a new scope, and the entire block is treated (from the grammar's perspective) as a single statement.That one simple rule means that:
\at the end of the line - if the language even supports that syntax)The one thing I would suggest is to prohibit braceless control statements (even the single-line variety) entirely.
You don't necessarily need to use the same curly brace characters as C-inspired languages. Feel free to pick some other delimiter.
You could even allow the delimiters to be changed. All that is required is that the modified delimiters result in unambiguous grammar. If you were to replace every instance of
{in your grammar with§and every instance of}with¶, you'd still be unambiguous.4
u/PercyLives Jun 04 '26
I use both Python and Ruby a fair bit. Here is what I would do if I were designing a language.
I would do exactly what Python does: use colon to open a block and indentation to define it.
But this is unpopular with a lot of people for some reason, and it has issues with pasting and code-generation (maybe that’s the reason). So I would make explicit blocks à la Ruby an option as well. In this case there would be no colon and the block would end with “end” and indentation would be ignored.
10
u/Inconstant_Moo 🧿 Pipefish Jun 04 '26
I like syntactic whitespace, because even when I'm working in a language without it, I'm reading the indentation and not the braces. Whereas the compiler is ignoring the whitespace and reading the braces. This means that I have to maintain them in sync, and when they say different things I have a problem.
5
u/GoblinToHobgoblin Jun 04 '26
If you use
.as a terminator, choose a different character for struct access, please1
u/Iprelliat Jun 04 '26
Hmm. One thing I definitely like about brace-scoped languages is that they usually follow a simple, consistent rule that block-expressions are followed by a single statement, and braces group multiple statements into one.
It allows for the convenience of:
Without any extra syntax.
Additionally, in your example, wouldn't
action2need two dots as it's escaping two levels of indentation?Ultimately, the purpose of indentation defining blocks is so that the compiler reads code the way humans like to write it. Requiring a dot just adds some syntactical redundancy to prevent the problems this causes. However, different humans like to structure code differently. Some wish to put statements on the same line, or spread one statement over multiple lines. Some indent multi-line expressions, others do not. Just look at different language auto-formatters (prettier, gofmt, etc) to see how opinionated they are!
If you think you can create a significant-whitespace language that caters to all the formatting opinions humans like, go fully with it, and don't worry about the dots! Tooling and compiler checks can pretty easily spot or even auto-fix copy-paste problems.
Finally, I may recommend considering ; instead of . if it's convenient on most keyboards. ; is much more familiar to most developers as a way of ending a statement, which is quite similar.
1
u/brucejbell sard Jun 04 '26
Interference with field notation is a non-starter for me; I care much more about field notation than line formatting.
My tack:
I prefer to keep the {curlies}, as well as redundancy in less common cases.
1
u/cb4ch Jun 06 '26
I’m planning on implementing my own language at some point, and have had similar thoughts on the different merhods. What I think I’ll personally do is.. support all of it.
And what I mean by that more specifically is that a block can be in braces, which will make sure a program will be valid even without any newlines.
It will also support intendation, no matter what number of spaces _or_ tabs you prefer, simply going by the diff in whitespace count compared to the previous line (if any).
And as a third alternative, an empty line will end the block in case there is no intendation at all. This will basically make blank lines invalid inside a block _unless_ you use braces or proper intendation.
That should cover lots of personal preferences - unless I’ve missed some obvious flaw…. 😅
1
u/kenjiArthur Jun 08 '26
Personally, I like indentation as the main visual structure, but I’m not completely sure about using a period to end blocks.
My main concern is that a period already feels like accessing an object's property or sentence punctuation, so using it as a block terminator could be easy to miss, especially in nested code. It is not that it would be unreadable, but I think it may become a bit "subtle", you know? At the same time, I really like the idea of the compiler checking indentation as a safety feature, it is also somehow "elegant". That feels like a nice middle ground to me as the code stays visually clean and the compiler can still catch weird formatting mistakes.
So for me, indentation plus some kind of safety check sounds really good. I would jsut be careful with using "." specifically, because many programmers already associate it with accessing object properties, and it feels very small and easy to miss.
3
u/BlackForrest28 Jun 04 '26
In Modula 2 the BEGIN is implicit contained in the IF Statement and not to be typed. But the ALL UPPER CASE for keywords is really annoying and from my point of view one of the biggest mistakes. But otherwise I really like this kind of block building. It's clear, not ambigous and sufficient simple to type.
1
u/L8_4_Dinner (Ⓧ Ecstasy/XVM) Jun 04 '26
Your ideas are sufficiently "weird" from a mainstream PoV (i.e. what 99% of developers are used to) that your design will create huge and unnecessary barriers to entry. If your goal is to build a language for only your own use, then I suggest that you are on the right track. Otherwise, I'd suggest that you stop thinking about the syntax and start thinking about the language.
2
u/tobega Jun 04 '26
I like having enforced formatting, no choice. So a dot or semicolon or something at the end, sure, then a formatter that makes the indent. So mandatory but redundant indentation, auto-formatted.
2
u/BothWaysItGoes Jun 04 '26
None of that matters for me in a language.
2
u/mamcx Jun 04 '26
Look at https://pyret.org & https://nim-lang.org both alike python.
To judge, you should look for bigger programs and different usages to see if the idea make sense.
P.D: A lot of the problem with python is that allow both tabs AND spaces but mixing at the same time is the trouble. Use one or the other and the main issue disappear. Add end and copy/paste issue is gone. Need to check other constructs but I think this is the main gist.
2
u/EggplantExtra4946 Jun 04 '26
curly braces for blocks
semicolon as a statement terminator, but optional for the last top-level statement or the last statement of a block, never needed after a closing curly brace
dot for struct dereference and namespace separator
whitespace for delimiting tokens
2
u/Normal-Reaction5316 Jun 04 '26
As for curly braces being hard to type on some international keyboard layouts, some languages I've used in the past supported alternative delimiter, so e.g. "{" and "}" could alternatively be typed as "(." and ".)" with the exact same effect. Pairs had to match, though. I'm not saying you should do this - just offering it as one perspective.
1
u/mcherm Jun 04 '26
Seems to me like it has the disadvantage of requiring random punctuation (when the actual visual demarkation of blocks is via indentation) while also making the amount of indentation significant (the usual complaint about indentation-delimited blocks). PLUS it makes the "." character take on yet another meaning, which can potentially be confused with field access.
In my opinion, "Indentation is a visually clean way to demarcate blocks" (to use your words). Just go ahead and use it -- there are plenty of languages which do so. I do not know of any language in any editor where I copy blocks of content around and paste them without correcting the indentation, counting on delimiters to denote blocks correctly. (Most modern IDEs will auto-correct the indentation for me.)
Furthermore, if you think that "base . field" shouldn't work and "base. field" is ugly, then prohibit them! Require "base.field" for field access if you want.
1
u/luizlls_ Jun 04 '26
One thing I thought about doing in my language is having a single
endthat closes many blocks if end is the last thing you wantSo, instead of having something like this:
You can just have a single
end:but you would still need to use multiple
endif it's not the last expression1
u/Falcon731 Jun 04 '26
The approach I took for my language is to primarily define blocks purely by indentation, but to also allow end statements ‘end if’ ‘end fun’ etc, and the compiler enforces that the correct block kind is being terminated.
The idea is that for short blocks indentation works well. But for longer blocks (especially if things go off screen, or where you have a cliff edge of multiple blocks all finishing at the same place) it can get hard to track. So adding explicit end markers in those situations can help make things more readable and robust.
1
u/perlgeek Jun 04 '26
I'd like to offer a slightly different perspective: whatever you design in a language, make sure that the compiler can also act as formatter, and establish a strong culture that it's always used.
When you actually use such a language, it eliminates basically all discussion about formatting, because everybody[tm] just uses the formatter.
As the language designer, you then don't need to spend too much mental energy about where you allow whitespace and not, because there's a standard way that must work, and everything else is a second-class citizen anyway, and doesn't justify extended discussion.
1
u/chkas Jun 04 '26
In my programming language, Easylang, a period delimits a block. The auto-formatter then ensures proper indentation.
1
u/1984balls Jun 04 '26
Scala originally used only braces to define blocks, but now in Scala 3.x, you can use indentation as well as braces. Some of my code ends up using both at the same time
I like it like this as it gives you the grounding and countability of braces but the ease for reading of indentation.
1
u/sazasoo Jun 08 '26
I don't think using a period as a block terminator is a good solution since it's too small and easily goes unnoticed. Characters with more visual weight are probably better, like --, ;, or even : could work better depending on how those symbols are used elsewhere in your language.
1
u/Germisstuck CrabStar Jun 04 '26
Personally I'm a fan of let-in, in my language it looks like this:
let y: 5 in y - 100
1
u/initial-algebra Jun 04 '26
An advantage of significant newlines and indentation is that good parser error recovery, matching the intent of the programmer, becomes trivial. It's pretty hard to forget or accidentally delete a newline, and it's impossible to fail to close an indented block. All languages should be designed for live editing feedback via LSP these days.