Questions tagged [language-design]
A tag for questions related to the design of any aspect of programming languages.
1,403
questions
0
votes
1
answer
75
views
Can i define type-level naturals in F#, then use them in C#?
In an earlier question I wondered if there is some way in C# to implement matrix dimensionality as a type-level property. That is, I wanted to be able to declare a generic family of classes of the ...
1
vote
1
answer
44
views
Defining an infinite family of classes "MatrixMxN" in C#
Suppose I was writing a linear algebra library in C#, and I wanted to implement matrix multiplication. Of course, two matrices A and B can only be multiplied to form AB if A has as many columns as B ...
3
votes
2
answers
170
views
Why does std::optional not use a sentinel value to represent an empty optional?
I know this ship has sailed due to ABI breakage it would require, but I wonder why originally implementations did not decide to use some magic bit pattern for std::string, std::vector, etc... to ...
0
votes
0
answers
40
views
Why is the std::iterator_traits::iterator_category for pointers not std::contiguous_iterator_tag?
iterator_traits is specialized for pointers as
namespace std {
template<class T>
requires is_object_v<T>
struct iterator_traits<T*> {
using iterator_concept = ...
0
votes
1
answer
67
views
Which programming languages don't treat if as syntax?
In which programming languages is a conditional block (if) not syntactically different than a function invocation, including any "else" block of code to be executed in the false condition? ...
20
votes
3
answers
2k
views
Why does ranges::for_each return the function?
The legacy std::for_each returns function as the standard only requires Function to meet Cpp17MoveConstructible according to [alg.foreach]:
template<class InputIterator, class Function>
...
12
votes
1
answer
1k
views
Why does C++ have no std::invocable_r concept?
C++20 added concepts, and the standard library includes quite a few of them. One concept in particular caught my eye: std::invocable which validates that a functor can be invoked with a set of ...
1
vote
1
answer
61
views
Why is std::make_shared<T> not a static function of std::shared_ptr<T>, i.e. std::shared_ptr<T>::make?
This is a question about understanding a design decision, not a complaint about bugs or flaws.
In the C++ standard library, the function to create a shared pointer and its object is a straight ...
4
votes
1
answer
92
views
Why is the KeyEqual of std::unordered_map not used by its operator==?
In the following code, I have defined template arguments Hash and KeyEqual for unordered_map. I expect the output to be 1 1 1 1 but it is actually 1 1 0 1. Why does this happen? Is it because std::...
0
votes
1
answer
71
views
Discriminating between a Lambda Parameter List and a Grouping Expression in a Recursive Descent Parser
I am writing a small scripting language for a project I am working on. I wrote a simple recursive-descent parser for it (similar to the one in the Crafting Interpreters). I wanted to add support for ...
2
votes
1
answer
68
views
Why can't most defaulted operators have a placeholder return type, i.e. auto?
C++20 has made it possible to default comparison operators, including the three-way comparison like this. <=> can have a deduced return type, but other operators can't:
struct S {
friend ...
0
votes
0
answers
104
views
LLVM pointers dereference
In my main function, I have allocated an i8 pointer:
%a = alloca i8*, align 8
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @1, i32 0, i32 0), i8** %a, align 8
Is it possible to:
assign a ...
1
vote
1
answer
130
views
When should I overload the co_await operator?
I have recently discovered that the following code is legal:
struct S {
void operator co_await();
};
Why is co_await an overloadable operator? Why would I want to overload it, and what is the ...
16
votes
4
answers
2k
views
What is the purpose of _t aliases and _v variable templates for type traits?
There are a lot of *_v and *_t suffixes, like std::is_same_v, std::invoke_result_t, result_of_t and milions of other such functions.
Why do they exist at all? Is it beneficial in any context to expose ...
0
votes
1
answer
63
views
Declare string type for parameter in LLVM
How would I declare a string parameter in the LLVM C++ API? For example, to declare a double parameter, I would do this:
argTypes.push_back(Type::getDoubleTy(*context))
Is it possible to do this for ...
2
votes
1
answer
232
views
Why aren't traits Sized by default?
I defined a trait ReadTag which contains a function that returns Self but such yields an error:
trait ReadTag {
fn read_out(buf: &mut &[u8]) -> Option<Self>;
}
error[E0277]: the ...
1
vote
1
answer
124
views
Why does Rust require return for early returns, but not at the end? [closed]
Why are implicit returns at the end allowed, but early returns require the return keyword?
fn bar() -> u64 {
if true { 1 }
else { 0 }
}
This is is semantically equivalent but raises an ...
2
votes
5
answers
268
views
Unpack / pack operator
I'm looking to find various ways that a packing / unpacking operator is implemented. As an example:
*[1,2,3] --> 1,2,3 (one array scalar value unpacked to three values)
*1,2,3 --> [1,...
1
vote
1
answer
62
views
Capturing a group with a generic SQL dialect
I would like to define an expression of the grammar:
=expr + #native(...)
expr can be any valid expression in our grammar, and this currently works. However, #native(...) is a valid SQL statement in ...
1
vote
0
answers
34
views
Non-local returns from constructs that do not have a call stack entry
I'm implementing named non-local returns in an AST interpreter written in Rust, and I have principles for closure NLR laid out. However, I'd like all expressions to support being labeled and returned ...
0
votes
0
answers
167
views
Why was it necessary to introduce ref/reactive in Vue 3?
This question is NOT about "ref vs. reactive", which is what floods the results when I try to search for the info I'm after. This question is about WHY they were introduced in Vue 3 in ...
0
votes
2
answers
49
views
Why aren't first-party, language-specific, dependency-managing build tools ubiquitous?
I've only seen this with C# (MSBuild as of 2017, usually invoked through dotnet CLI), Rust (Cargo, technically a "build system" rather than a "build tool"), and Go (go CLI as of v1....
0
votes
0
answers
65
views
Why can't I call a pointer method on a temporary object?
type A struct{}
func (*A) F() {}
func main() {
a := A{}
a.F() // OK
(&a).F() // OK
(&A{}).F() // OK
A{}.F() // Error: cannot call pointer method F ...
41
votes
3
answers
5k
views
Why is C++20's `std::popcount` restricted to unsigned types?
The functions from P0553R4: Bit operations are constrained to only work on unsigned integers. The proposal does not give a reason for this constraint. I can see that this makes sense if the bit ...
0
votes
1
answer
47
views
Python and JavaScript: different default parameter behaviour [duplicate]
I am trying to solve a puzzle in python and javascript and came across a problem.
Below python and javascript are giving different outputs but they are both interpreter languages. Any idea why they ...
2
votes
1
answer
145
views
Why does the C++ language prohibit the usage of certain ALT Codes that are printable characters as identifiers?
Consider the follow code snippet:
struct vec2 {
int x;
int y;
};
constexpr vec2 Up{0,1};
constexpr vec2 Down{0,-1};
constexpr vec2 Left{-1,0};
constexpr vec2 Right{1,0};
The above code ...
-1
votes
1
answer
195
views
Why is access to private static members through a subclass forbidden?
From MDN Web Docs:
There is a restriction on private static fields: only the class which defines the private static field can access the field. This can lead to unexpected behavior when using this. ...
0
votes
2
answers
143
views
Why do instance fields belong to the instances instead of their prototype?
In the JavaScript class syntax introduced in ES6 (static properties are accessible from the class, instance properties are accessible from the instances):
static fields belong to the class;
static ...
0
votes
2
answers
153
views
Type theory with an Any/Variant type
Let's say I have a type system that has has three primitive types in addition to the null value:
null
bool
num
string
Additionally, there is a typed array for each of the types, so we now have: null,...
1
vote
1
answer
92
views
Why did Matlab choose saturation arithmetic for integers?
Matlab implements saturation arithmetic instead of wrapping on overflow for integers. While understanding the difference between the two, but not necessarily finer nuances, I wonder what was the ...
4
votes
1
answer
103
views
Why can I access the base class aliased types from the derived class?
In my development journey, today I found something that I do not understand why happens.
This should represent a generic a base unit in the SI:
template<Ratio r, Symbol s>
struct base_unit {
...
-3
votes
1
answer
46
views
Is the keyword END a noun or a verb?
Quite a few programming languages use the keyword END to mark the end of a structured statement or the end of a procedure declaration etc. Is this word to be read as a noun or as a verb (in imperative)...
4
votes
3
answers
325
views
Why is extern required for variables but not functions?
In C, both variables and functions have external linkage at file scope by default. Why is the keyword extern only required for variables but not functions that are defined elsewhere? Note that there ...
1
vote
1
answer
610
views
Why do we need the concept keyword in C++20?
Concepts are great, don't get me wrong, but why do we need another keyword for this?
Consider the following example:
#include <type_traits>
template <typename T>
concept UnsignedConst = ...
1
vote
1
answer
107
views
Why does the standard allow empty but non-null shared_ptr? [closed]
This question is different from std::shared_ptr which is empty but not null.
That question is about solving a particular problem, while I'm asking about the rationale and design reasons behind a ...
1
vote
1
answer
208
views
Why does the TypeScript compiler compiles enums to functions?
TL;DR
The Typescript compiler compiles enums to functions, and it does so in a seemingly peculiar way. What are the benefits?
Note:
This question is not opinion-based. I am looking for objective, ...
7
votes
1
answer
943
views
Given C++23 relaxations on constexpr, can't constexpr be the default?
The keyword constexpr enforced pretty tight restrictions on functions on its introduction into the C++11 standard. These restrictions were loosened with C++14 and C++20 (most noteworthy):
C++14 ...
3
votes
2
answers
185
views
Abstract Data Type definition in Python
Consider the following Abstract Data Type (using Haskell syntax):
data Expr = Literal String | Symbol String | And [Expr] | Or [Expr]
In Python, one can make use of dataclasses and inheritance to ...
1
vote
2
answers
138
views
Concept of "Excel [Blank] Cell" in any programming language?
Excel has a Blank cell which has some interesting properties when it comes to calculations:
In the below we will assume cell A1 is blank.
A blank cell equals another blank cell: =A1=A1.
A blank cell ...
3
votes
2
answers
183
views
What is the rationale to disallow type traits in type-constraints in addition to concepts?
The language specification, when introducing Concepts, implicitly disallows type traits to be used in type-constraint contexts in lieu of concepts:
#include <type_traits>
// doesn't work ...
1
vote
1
answer
41
views
What is 'Should the conditional mechanism be an integral part of the exit' means when it comes the language design?
I had an assignment for my Programming Language homework which makes us to experiment over the language Lua. One of the aspect of the language we required to experiment over is should the conditional ...
3
votes
2
answers
636
views
Why use trait bounds in struct definitions with generic type parameters?
I can define a struct type that uses a generic type parameter with a trait bound:
struct MyStruct<T: Clone> {
field: T,
}
This prevents me me from instantiating MyStruct with a generic type ...
0
votes
0
answers
286
views
YACC Parser getting an error at line 1 even if test program is empty?
I have been trying do my homework which is language design with Lex and YACC. My assignment is building a simple parser with YACC. But my problem is the test file I send to my parser always returns a ...
0
votes
1
answer
241
views
Why does a for loop have three parts?
A typical for loop looks like this
for (int i = 0; i < max; ++i){
DoStuff();
}
As far as I can tell, the above is equivalent to
for (int i = 0; i < max;){
DoStuff();
++i;
}
The ...
0
votes
1
answer
42
views
Specialized reduce/fold methods for specific element types
Most language APIs these days offer ways to fold or reduce iterables (or observables) into a single element. For example, in the Xtend "dialect" of Java I could fold a set of elements into a ...
0
votes
0
answers
28
views
Why does JavaScript have both `.prototype` and internal `.[[prototype]]`?
This is about language design of JavaScript: why does JavaScript have both .prototype and internal .[[prototype]]?
Why it doesn't always use read-write property .prototype instead of having hidden ...
0
votes
2
answers
92
views
Is it possible to have nested functions without closures?
My understanding of closures so far is that they combine "open" functions with their surrounding scope, essentially making them closed expressions.
I've seen several examples of how this is ...
1
vote
1
answer
124
views
Why do some languages specify a function or subroutine preamble? [closed]
I am not familiar with compiler design or implementation problems outside of an academic introduction many years ago.
In C I might define a function thusly:
int foo(int bar) {
return bar * 2;
}
...
1
vote
0
answers
48
views
language for data-driven programming
I'm wondering if there is a language that supports programming in the following style that I think of as "data driven":
Imagine a language like C or Python except that it's possible to ...
1
vote
0
answers
46
views
Scripting language/environment with deterministic execution, sandboxing, and resource limits
For an educational game, I'm looking for a scripting language which is cross-platform deterministic (e.g. no hardware floating point), sandboxed, and resource-limited.
The difficult part, it seems, is ...