Questions tagged [language-design]

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

1363 questions
39
votes
3 answers

Why does the implicit copy constructor calls the base class copy constructor and the defined copy constructor doesn't?

Consider a class hierarchy where A is the base class and B derives from A. If the copy constructor is not defined in B, the compiler will synthesize one. When invoked, this copy constructor will call the base class copy constructor (even the…
Vincenzo Pii
  • 18,961
  • 8
  • 39
  • 49
39
votes
8 answers

Read/Write Python Closures

Closures are an incredibly useful language feature. They let us do clever things that would otherwise take a lot of code, and often enable us to write code that is more elegant and more clear. In Python 2.x, closures variable names cannot be…
Benson
  • 22,457
  • 2
  • 40
  • 49
38
votes
6 answers

What exactly is or was the purpose of C++ function-style casts?

I am talking about "type(value)"-style casts. The books I have read pass over them quickly, saying only that they are semantically equivalent to C-style casts, "(type) value", and that they should be avoided. If they mean the same thing an old-style…
Cplusplusstudent
  • 383
  • 1
  • 3
  • 4
37
votes
3 answers

Why no stored type properties for classes in swift?

Working through The Swift Programming Language, I was surprised to see that, unlike structures and enumerations, classes do not support stored type properties. This is a common feature of other OO languages so I assume there was a good reason they…
John Mullaney
  • 370
  • 3
  • 7
36
votes
2 answers

Datatype promotion for dependently challenged

After reading through the ghc 7.4. pre-release notes and the Giving Haskell a Promotion paper, I'm still confused on what you actually do with promoted types. For example, the GHC manual gives the following examples on promoted datatypes: data Nat =…
aleator
  • 4,436
  • 20
  • 31
35
votes
5 answers

PEG for Python style indentation

How would you write a Parsing Expression Grammar in any of the following Parser Generators (PEG.js, Citrus, Treetop) which can handle Python/Haskell/CoffeScript style indentation: Examples of a not-yet-existing programming language: square x = x…
Matt
  • 17,290
  • 7
  • 57
  • 71
34
votes
8 answers

In C/C++ why does the do while(expression); need a semi colon?

My guess is it just made parsing easier, but I can't see exactly why. So what does this have ... do { some stuff } while(test); more stuff that's better than ... do { some stuff } while(test) more stuff
justinhj
  • 11,147
  • 11
  • 58
  • 104
34
votes
1 answer

Why does a collection initializer expression require IEnumerable to be implemented?

Why does this generate a compiler error: class X { public void Add(string str) { Console.WriteLine(str); } } static class Program { static void Main() { // error CS1922: Cannot initialize type 'X' with a collection initializer …
Timwi
  • 65,159
  • 33
  • 165
  • 230
34
votes
4 answers

Is it possible to create a quine in every turing-complete language?

I just wanted to know if it is 100% possible, if my language is turing-complete, to write a program in it that prints itself out (of course not using a file reading function) So if the language just has the really necessary things in order to make…
sub
  • 2,653
  • 3
  • 27
  • 29
34
votes
1 answer

Mathematica: Unevaluated vs Defer vs Hold vs HoldForm vs HoldAllComplete vs etc etc

I'm bewildered by all the built-in Mathematica functions that purport to prevent evaluation in some way: Unevaluated, Defer, Hold, and over half a dozen of the form Hold*. The Mathematica documentation just explains each function in isolation…
dreeves
  • 26,430
  • 45
  • 154
  • 229
34
votes
3 answers

Why can't an interface implementation return a more specific type?

If an interface specifies a property or method to return another interface, why is it not allowed for implementations of the first interface to "change" the return type into a more specific type? Let's take an example to illustrate: interface…
Isak Savo
  • 34,957
  • 11
  • 60
  • 92
33
votes
5 answers

Why does the local variable of an enhanced for loop have to be local?

According to the Java Language Specification, § 14.14.2, the variable of an enhanced for loop must be local to the loop. In other words, this compiles: for (State state : State.values()) { // do something for each state } but this does…
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
33
votes
2 answers

Closures in Scala vs Closures in Java

Some time ago Oracle decided that adding Closures to Java 8 would be an good idea. I wonder how design problems are solved there in comparison to Scala, which had closures since day one. Citing the Open Issues from javac.info: Can Method Handles be…
soc
  • 27,983
  • 20
  • 111
  • 215
32
votes
7 answers

How do multimethods solve the namespace issue?

I am researching programming language design, and I am interested in the question of how to replace the popular single-dispatch message-passing OO paradigm with the multimethods generic-function paradigm. For the most part, it seems very…
Patrick Li
  • 672
  • 4
  • 11
32
votes
2 answers

Why does !new Boolean(false) equals false in JavaScript?

From the jQuery documentation on JavaScript types comes this snippet of code describing the behavior of strings when converted to booleans (that topic is not related to this question, but it's just where I found the code): !"" // true !"hello" //…
Michiel van Oosterhout
  • 22,839
  • 15
  • 90
  • 132