Questions tagged [syntax]
For questions relating to the syntax, or overall structure, of programming languages.
115
questions
1
vote
1
answer
85
views
What are the potential consequences of allowing decimal-point-less number literals to be interpreted as floats?
In some languages, a number literal's type can be inferred from nearby expressions. For example, 1u64 + 2 might give a u64 value ...
4
votes
1
answer
127
views
Should bitwise operations have dedicated operators? [closed]
Many programming languages have built-in support for bitwise operations, often using C's syntax:
~ = NOT
& = AND
...
3
votes
1
answer
113
views
Possible ways to determine reach of implicit lambda expressions
When defining an inline function, even the shortest way to do so usually requires naming its argument (unless you're going for the point-free style and you have an expression that returns a function).
...
2
votes
1
answer
151
views
What if all static methods in Java or C# could be extension methods just by default?
C# 3.0 introduced extension methods to "enhance" behavior of existing types without interface bloating to avoid modifying/breaking existing interfaces. This was the first time I learned of ...
26
votes
2
answers
3k
views
Studies on learnability of braces vs. indentation for code blocks for beginners?
The discussion whether using indentation for code blocks is better or worse than using braces is an old one, but I don't want to rehash that discussion here (for reference, there was a fairly recent ...
2
votes
1
answer
994
views
Why do we put imports at the top of files? [closed]
Most of the popular programming languages I know of either require or strongly encourage import statements to be at the top of the file, before any of the ...
6
votes
1
answer
136
views
What's the benefit of using different tokens for accessing nested namespaces/modules versus properties?
In JavaScript, accessing items from a module is done using ., e.g. moduleBar.moduleFoo.functionFoo, which looks exactly the same ...
29
votes
11
answers
18k
views
Why do so many programming languages not have a "built-in" way to do simple math functions?
Note 1: My question is not about the factorial function. It's about "simple math functions" that high-school level pocket calculators can do, but most programming languages cannot do without ...
21
votes
4
answers
4k
views
Why do many template languages have `for-else` statements?
I've noticed that many template engines have a mechanism for detecting that a loop is not occurring.
Although their names and syntax vary, they all have this similar structure.
For example:
Jinja
<...
12
votes
7
answers
4k
views
Are there any situations in which it would make sense to return a never type?
Some languages (Rust is the first one that comes to mind) have a "never" type. This is represented in Rust as !, and represents a function that never ...
31
votes
3
answers
11k
views
Why did Rust require macro names to have an exclamation point at the end?
Why did the designers of the Rust programming language require that macro names have to end with an exclamation point?
I observe that both Common Lisp and Emacs Lisp language variants do not appear, ...
17
votes
6
answers
8k
views
Does Python's semicolon statement ending feature have any unique use?
Python does not usually use explicit line-ending characters; the parser can almost always figure out where a statement ends from the positioning of newlines. However, Python's syntax allows you to use ...
16
votes
2
answers
2k
views
How do lexers/parsers distinguish between nested generics and bitshifts?
The lexical grammar of Java has a special case for the the > character. Normally, tokens are formed based on the longest-match rule, so that an input string of <...
2
votes
4
answers
359
views
What evidence exists on using & and | as logical operators instead of && and ||?
I’m working on a small, dynamic language that isn’t going to include bitwise operators so I was thinking it was a bit unnecessary to use && and ...
4
votes
6
answers
1k
views
Why tag function definition with def, fn, fun, func or function etc?
What makes it hard for compilers/interpreters that we need to tag the function definition with keyword fn/fun/func/function?
I can understand that types for numbers: int, int32, float32, float64... ...
8
votes
2
answers
764
views
Why do many languages use square brackets for array indexing?
A lot of languages use square brackets for array indexing, and round brackets (also known as parentheses) for function calls. For example, in C:
...
42
votes
6
answers
12k
views
Why do programming languages use the asterisk * for multiplication?
Having had very little math(s) education I'm trying to bring myself up to speed for university, which currently involves teaching myself the times tables, where X ...
27
votes
6
answers
11k
views
Why do programming languages use delimiters (quotes) for strings?
Almost every programming language requires strings (or char* or equivelent) to be marked with quotes. Few languages allow other delimiters, many languages allow ...
2
votes
0
answers
80
views
Is it more readable to declare the return type of a function on the left side of it's name? [duplicate]
A lot of C-like programming languages (Java, C#, ...) use the following syntax for defining functions:
returnType functionName(parameters...) {
}
Is there a ...
6
votes
5
answers
469
views
Declaring infix operators like Haskell's in other languages?
Haskell has support for declaring custom infix operators, including their precedence and associativity. In addition, any identifier can be used as binary infix operator by placing it between ...
3
votes
4
answers
227
views
Common postfix representations of pointer/reference operations
In some C-based languages (including C/C++ themselves and Rust), taking pointers/references and dereferencing is done with prefix operators & and ...
9
votes
1
answer
1k
views
In Python, why isn't it a syntax error when a list of strings is defined without comma separators?
When coding in Python, I found that defining a list of strings without separating the strings with a comma is not a syntax error. When running this code:
...
2
votes
1
answer
158
views
What are the exact requirements of not requiring forward declarations everywhere?
Some languages like C and C++ require defining the names before using them, though the definition could sometimes be only a header specifying something is a struct or function without the body. Some ...
2
votes
1
answer
202
views
What syntactic ambiguities can arise in a language with optional semicolons for statement or expression separators?
What syntactic ambiguities can arise in a language with optional semicolons for statement or expression separators?
Let's say that that the syntax for this language generally follows the C syntax and ...
2
votes
3
answers
191
views
What are pros and cons of adding special syntactic sugar for decorators? [closed]
Some languages have syntactic sugar for decorators (like Python), but some languages do not (like Go).
PSL Some languages do not have decorators at all.
So, what are pros and cons of syntactic sugar ...
18
votes
7
answers
4k
views
How can the language or tooling notify the user of infinite loops?
I'm trying to teach a friend to code in Python. I've noticed that whenever they write a while loop with an integer increment, such as the one shown below:
...
2
votes
1
answer
90
views
Languages with source include (AST or concatenated) [closed]
Some languages allow you to import local (file system) files normally via some directives like import:
In Dart: ...
14
votes
4
answers
3k
views
Could function parameters be placed inside names?
While in the shower thinking about my code, my mind went to C#'s type parameters, and I wondered what stops the type param from being placed in the middle of the function name like this:
...
30
votes
10
answers
8k
views
Avoiding student confusion about the meaning of `x == a or b`
I'm designing a language intended to teach kids and other beginners how to program. It's just a fork of Python with a few extra features to make it easier to learn.
I've noticed students make one ...
4
votes
1
answer
126
views
Do let bindings need an assignment operator for disambiguation?
Let's say I want to allow (conservative) let bindings, with optional type ascription. Most imperative languages would put a = or ...
4
votes
1
answer
152
views
What sort of "syntax error recovery" is possible for this indentation based language?
I am thinking of how to write the parser/compiler so it works as a VSCode Language Server Extension, so I can have it show syntax errors, do autocomplete, and other things, in VSCode. One thing that I'...
9
votes
4
answers
379
views
What are the advantages of keyword based syntax versus punctuation based syntax?
Some languages such as Lua and Assembly Basic use keywords for syntax. This includes spelling out words such as do and end in ...
2
votes
3
answers
182
views
Pros and cons of generalized `nil`-coalescing operators
Many languages extend their "indexing" operator . to a nil-coalescing variant ?. to deal with indexing chains such as <...
5
votes
6
answers
309
views
Ways to have operators for both normal and floor division
I want to have an operator for true division (like / in python) giving a float as the result and floor division (like // in ...
-4
votes
1
answer
212
views
What are the disadvantages of including foreach loops? [duplicate]
Yes, FOR loops. Those great things. There are good for shortening code. But there may be downsides I'm not aware of. I'm referring to Python style FOR loops. So, what are the disadvantages of ...
4
votes
2
answers
147
views
Is it necessary to provide singleton as language keywords?
Background
The singleton pattern is a widely used design pattern in the OOP language.
I'm considering adding a built-in singleton declaration to my language.
In Scala and Kotlin, we can use ...
11
votes
8
answers
3k
views
What are the pros and cons of allowing keywords to be abbreviated?
A language that I use (M) has this nifty feature that allows you to abbreviate most keywords to a single letter. For instance, the following code:
...
9
votes
6
answers
2k
views
What syntax could be used to implement both an exponentiation operator and XOR?
C does not have an exponentiation operator, so that freestanding implementations that lack the math libraries could lack exponentiation as a whole, and still be permissible by the C standard. As a ...
21
votes
7
answers
6k
views
Why do most languages use the same token for `EndIf`, `EndWhile`, `EndFunction` and `EndStructure`?
In my programming language, AEC, I am using keywords EndIf and EndWhile with the same meaning as in SmallBasic, and I am also ...
7
votes
3
answers
224
views
Loop syntax for inclusive and exclusive end boundaries
While the C-style for loop syntax is badly designed from a don't repeat yourself perspective, it does offer an advantage over ...
16
votes
5
answers
1k
views
What are the downsides of supporting chained comparison operators?
Some languages, such as Python, support chaining comparison operators, such that x < y < z is equivalent to ...
4
votes
2
answers
317
views
Does a simple syntax stack based language need a parser?
I have been working on a standard for a simple stack based language for a few weeks now. It has an extremely simple syntax.
...
5
votes
3
answers
339
views
What are some different approaches for inline assembly, and what are their pros and cons?
In my programming language that compiles to WebAssembly, I am using the following syntax for inline assembly:
...
15
votes
9
answers
462
views
What are some different approaches to raw string syntax, and what are their pros and cons?
Many languages include a "raw string" literal type, intended for arbitrary (or as close as possible to arbitrary) strings of characters (and/or bytes), ignoring escape sequences and the like....
5
votes
4
answers
255
views
How do languages handle bitwise flags?
In most languages, enum types intersect with number types, implicitly converting between them so you can do ...
8
votes
3
answers
128
views
How can single-item tuples be disambiguated from the grouping operator?
While single-item tuples aren't particularly useful, disallowing them entirely, or allowing them but with few to no valid representations in syntax, may also be undesirable. However, since tuples ...
4
votes
1
answer
101
views
Pros and cons of reusing variable and function syntax on class block
The inexistent ECMAScript 4 and ECMAScript 2015 took different paths. That's how you write a class in ES4 (similiar to Python (def), Rust (...
10
votes
3
answers
440
views
How do language designers determine what feature flags are part of the standard library and what are part of syntax?
Java has the @Override annotation. This annotation, when applied on a method, basically says that this method is intended to be an override of a superclass method. ...
2
votes
3
answers
95
views
Pre-increment and post-increment and augmented assignment but no post-augmented assignment?
In C-style languages we have:
int x = 10;
x++; // x is now 11 but the expression returns 10
...
7
votes
6
answers
144
views
Possible approaches to dialect selection
In my language, I'd like to allow users to define custom 'dialects'. A dialect is, essentially, an object capable of parsing a source code in a particular grammar and returning a common high-level ...