Questions tagged [language-design]

A tag for questions related to the design of any aspect of programming languages.

1363 questions
70
votes
2 answers

Purpose of "let expression" (LetExpr) in the Java compiler?

The Java compiler seems to have support for let expressions in com.sun.tools.javac.tree.* (look for LetExpr). One comment in JCTree even mentions some syntax (let int x = 3; in x+2) which of course is not accepted by the grammar of the language and…
soc
  • 27,983
  • 20
  • 111
  • 215
70
votes
5 answers

Performance of "direct" virtual call vs. interface call in C#

This benchmark appears to show that calling a virtual method directly on object reference is faster than calling it on the reference to the interface this object implements. In other words: interface IFoo { void Bar(); } class Foo : IFoo { …
Branko Dimitrijevic
  • 50,809
  • 10
  • 93
  • 167
70
votes
6 answers

Bounding generics with 'super' keyword

Why can I use super only with wildcards and not with type parameters? For example, in the Collection interface, why is the toArray method not written like this interface Collection{ S[] toArray(S[] a); }
mohsenof
  • 711
  • 1
  • 6
  • 5
70
votes
4 answers

Why is size_t unsigned?

Bjarne Stroustrup wrote in The C++ Programming Language: The unsigned integer types are ideal for uses that treat storage as a bit array. Using an unsigned instead of an int to gain one more bit to represent positive integers is almost never a good…
Jon
  • 5,275
  • 5
  • 39
  • 51
69
votes
9 answers

Why can't Java constructors be synchronized?

According to the Java Language Specification, constructors cannot be marked synchronized because other threads cannot see the object being created until the thread creating it has finished it. This seems a bit odd, because I can indeed have another…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
68
votes
4 answers

Why does the power operator in F# only work for floating point numbers?

I have never seen a language have exponent or power operator only taking floating point numbers? For example: 2 ** 2 throws an error The type 'int' does not support any operators named 'Pow' Are there valid reasons for this design decision?
unj2
  • 52,135
  • 87
  • 247
  • 375
68
votes
4 answers

Why is F#'s type inference so fickle?

The F# compiler appears to perform type inference in a (fairly) strict top-to-bottom, left-to-right fashion. This means you must do things like put all definitions before their use, order of file compilation is significant, and you tend to need to…
J Cooper
  • 16,891
  • 12
  • 65
  • 110
68
votes
4 answers

design of python: why is assert a statement and not a function?

In Python, assert is a statement, and not a function. Was this a deliberate decision? Are there any advantages to having assert be a statement (and reserved word) instead of a function? According to the docs, assert expression1, expression2 is…
cberzan
  • 2,009
  • 1
  • 21
  • 32
67
votes
5 answers

Why is adding null to a string legal?

The MSDN article on String Basics shows this: string str = "hello"; string nullStr = null; string emptyStr = ""; string tempStr = str + nullStr; // tempStr = "hello" bool b = (emptyStr == nullStr);// b = false; string newStr = emptyStr + nullStr;…
David Hodgson
  • 10,104
  • 17
  • 56
  • 77
66
votes
3 answers

Lua operators, why isn't +=, -= and so on defined?

This is a question I've been mildly irritated about for some time and just never got around to search the answer to. However I thought I might at least ask the question and perhaps someone can explain. Basically many languages I've worked in utilize…
qrikko
  • 2,483
  • 2
  • 22
  • 35
64
votes
5 answers

Why do the C++ language designers keep re-using keywords?

What is the main argument in favor of re-using short keywords (and adding context-dependent meanings) instead of just adding more keywords? Is it just that you want to avoid breaking existing code that may already be using a proposed new keyword,…
Andrew Wagner
  • 22,677
  • 21
  • 86
  • 100
64
votes
6 answers

Why are slice and range upper-bound exclusive?

I know that when I use range([start], stop[, step]) or slice([start], stop[, step]), the stop value is not included in the range or slice. But why does it work this way? Is it so that e.g. a range(0, x) or range(x) will contain x many elements? Is…
wap26
  • 2,180
  • 1
  • 17
  • 32
64
votes
8 answers

How does a stackless language work?

I've heard of stackless languages. However I don't have any idea how such a language would be implemented. Can someone explain?
rlbond
  • 65,341
  • 56
  • 178
  • 228
63
votes
5 answers

Is Lua based primarily on well-established programming-language ideas?

Lua occupies a good place in the space of languages that can be embedded. Are the primary ideas behind Lua's design new ideas from the implementors, or is Lua primarily a well-executed combination of well-established ideas? Comparison of properties…
Charles Stewart
  • 11,661
  • 4
  • 46
  • 85
62
votes
1 answer

Why does the TypeScript compiler compile its optional chaining and null-coalescing operators with two checks?

Why does the TypeScript compiler compile its optional chaining and null-coalescing operators, ?. and ??, to // x?.y x === null || x === void 0 ? void 0 : x.y; // x ?? y x !== null && x !== void 0 ? x : y instead of // x?.y x == null ? void 0 :…
dx_over_dt
  • 13,240
  • 17
  • 54
  • 102