language-design

How would you go about implementing off-side rule?

I've already written a generator that does the trick, but I'd like to know the best possible way to implement the off-side rule. Shortly: Off-side rule means in this context that indentation is getting recognized as a syntactic element. Here is the offside rule in pseudocode for making tokenizers that capture indentation in usable form...

How does your favorite language handle deep recursion?

I recently started learning Python and I was rather surprised to find a 1000 deep recursion limit (by default). If you set it high enough, about 30000, it crashes with a segmentation fault just like C. Although, C seems to go quite a lot higher. (The Python folks are quick to point out that you can always convert recursive functions t...

Why was the switch statement designed to need a break?

Given a simple switch statement switch (int) { case 1 : { printf("1\n"); break; } case 2 : { printf("2\n"); } case 3 : { printf("3\n"); } } The absence of a break statement in case 2, implies that execution will continue inside the code for case 3. This is not an ...

what ever happened to the 'entry' keyword?

While cruising through my white book the other day, I noticed in the list of C keywords. entry is one of the keywords on that list. It is reserved for future use. Thinking back to my Fortran days, there was a function of some sort that used an entry statement to make a second argument signature, or entry point into a function. Is this...

Re-implementing an interface that another interface already inherits

I see stuff like this a lot: interface A { ... } interface B : A { ... } class C : B, A { ...} Why would you specify that C implements interface A, when B already inherits A? Does it make any semantic difference or is it just a matter of style? (One of many examples is List<T> implementing IList<T> and ICollection<T>, while IList<T> ...

Could C++ have not obviated the pimpl idiom?

As I understand, the pimpl idiom is exists only because C++ forces you to place all the private class members in the header. If the header were to contain only the public interface, theoretically, any change in class implementation would not have necessitated a recompile for the rest of the program. What I want to know is why C++ is no...

Overhead of using bignums

I have hit upon this problem about whether to use bignums in my language as a default datatype when there's numbers involved. I've evaluated this myself and reduced it to a convenience&comfort vs. performance -question. The answer to that question depends about how large the performance hit is in programs that aren't getting optimized. ...

Why doesn't a swap / exchange operator exist in imperative or OO languages like C/C++/C#/Java...?

I was always wondering why such a simple and basic operation like swapping the contents of two variables is not built-in for many languages. It is one of the most basic programming exercises in computer science classes; it is heavily used in many algorithms (e.g. sorting); every now and then one needs it and one must use a temporary var...

Are there any static duck-typed languages?

Can I specify interfaces when I declare a member? After thinking about this question for a while, it occurred to me that a static-duck-typed language might actually work. Why can't predefined classes be bound to an interface at compile time? Example: public interface IMyInterface { public void MyMethod(); } public class MyClass //D...

Declarations, definitions, initializations in C, C++, C#, Java and Python

What do the terms mean in each of the above languages? Why do the languages differ (wherever they do, if at all they do) in this respect? ...

Duck type as syntactic sugar for reflection: Good or bad idea?

I've been thinking lately, would it be a good form of syntactic sugar in languages like Java and C#, to include a "duck" type as a method parameter type? This would look as follows: void myFunction(duck foo) { foo.doStuff(); } This could be syntactic sugar for invoking doStuff() via reflection, or it could be implemented different...

Why can't variable names start with numbers?

I was working with a new c++ developer a while back when he asked the question: "Why can't variable names start with numbers?" I couldn't come up with an answer except that some numbers can have text in them (123456L, 123456U) and that wouldn't be possible if the compilers were thinking everything with some amount of alpha characters wa...

Creating your own language

If I were looking to create my own language are there any tools that would help me along? I have heard of yacc but I'm wondering how I would implement features that I want in the language. ...

Unicode strings in process memory

What is the most preferred format of unicode strings in memory when they are being processed? And why? I am implementing a programming language by producing an executable file image for it. Obviously a working programming language implementation requires a protocol for processing strings. I've thought about using dynamic arrays as the ...

What are the trade-offs between languages that terminate statements with semicolons and those that don't??

Are there any benefits to languages that terminate statements with a semicolon (C, Perl, etc.) compared with those that don't (Python, Ruby, etc.), or vice versa? (Note to late-comers: the original title and question asked about "do you trust languages that don't use a semi-colon"; it was rewritten to be less argumentative. Some of the...

Why is the scope of if and delegates this way in c#

Inspired by this question I began wondering why the following examples are all illegal in c#: VoidFunction t = delegate { int i = 0; }; int i = 1; and { int i = 0; } int i = 1; I'm just wondering if anyone knew the exact reason why the language was designed this way? Is it to discourage bad programming practice, and if so why...

Typed FP: Tuple Arguments and Curriable Arguments

In statically typed functional programming languages, like Standard ML, F#, OCaml and Haskell, a function will usually be written with the parameters separated from each other and from the function name simply by whitespace: let add a b = a + b The type here being "int -> (int -> int)", i.e. a function that takes an int and return...

Do smarter compilers, languages, and frameworks make dumber programmers?

The more and more advanced compilers, languages, and frameworks we have that either automate tasks, or hide certain intricacies from us, do they ultimately make us dumber programmers? Jim C gives the following analogy of a calculator: Any one extract a square root lately, by using paper and pencil? We use a calculator or simple ...

Why doesn't Java support unsigned ints?

Why doesn't Java include support for unsigned integers? It seems to me to be an odd omission, given that they allow one to write code that is less likely to produce overflows on unexpectedly large input. Furthermore, using unsigned integers can be a form of self-documentation, as they indicate that the value that the unsigned int was i...

Why do C# and Java bother with the "new" operator?

Why does the new operator exist in modern languages such as C# and Java? Is it purely a self documenting code feature, or does it serve any actual purpose? For instance the following example: Class1 obj = new Class1(); Class1 foo() { return new Class1(); } Is as easy to read as the more Pythonesque way of writing it: Class1 obj...