language-design

Does any programming language support defining constraints on primitive data types?

佐手、 提交于 2019-12-06 03:26:33
问题 Last night I was thining that programming languages can have a feature in which we should be able to constraints the values assigned to primitive data types. For example I should be able to say my variable of type int can only have value between 0 and 100 int<0, 100> progress; This would then act as a normal integer in all scenarios except the fact that you won't be able to specify values out of the range defined in constraint. The compiler will not compile the code progress=200 . This

How-to: short-circuiting inverted ternary operator implemented in, e.g. C#? Does it matter?

感情迁移 提交于 2019-12-05 18:49:44
Suppose you are using the ternary operator, or the null coalescing operator, or nested if-else statements to choose assignment to an object. Now suppose that within the conditional statement, you have the evaluation of an expensive or volatile operation, requiring that you put the result into a temporary variable, capturing its state, so that it can be compared, and then potentially assigned. How would a language, such as C#, for consideration, implement a new logic operator to handle this case? Should it? Are there existing ways to handle this case in C#? Other languages? Some cases of

Why is there a sizeof… operator in C++0x?

痴心易碎 提交于 2019-12-05 18:37:23
问题 I saw that @GMan implemented a version of sizeof... for variadic templates which (as far as I can tell) is equivalent to the built in sizeof... . Doesn't this go against the second design principle: prefer libraries to language extensions? 回答1: From Variadic Templates (Revision 3) (N2080=06-0150), page 6: Although not strictly necessary (we can implement count without this feature), checking the length of a parameter pack is a common operation that deserves a simple syntax . Moreover, this

c# switch statement more limited than vb.net 'case' [closed]

风流意气都作罢 提交于 2019-12-05 12:10:55
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 8 years ago . I was reading an interesting article here and it made an interesting point about the 'case' statement in vb.net vs the 'switch'

Why does COBOL have both `SECTION` and `PARAGRAPH`?

会有一股神秘感。 提交于 2019-12-05 09:40:31
问题 Why does COBOL have both SECTION and PARAGRAPH ? Can anybody explain why the designers of COBOL created both SECTION s and PARAGRAPH s? These have been around since the initial release of COBOL so I suspect the real reason for their existence has long since gone away (similar to things like NEXT SENTENCE which are still in the language specification for backward compatibility but no longer required since the introduction of explicit scope terminators). My guess is that SECTION may have been

Would it make sense to have a 'constify' operation in C++?

女生的网名这么多〃 提交于 2019-12-05 06:31:05
Would it make sense to have a " constify " operation in C/C++ that makes a variable const ? Here is an example where it could be useful, where obviously we don't want to declare it const yet in the first line: std::vector<int> v; v.push_back(5); constify v; // now it's const Currently, without such a possibility, you'd have to introduce another variable to get the same effect: std::vector<int> v0; v0.push_back(5); const std::vector<int>& v = v0; That's more confusing since it adds a new name into the scope and you need to make it a reference to avoid copying the whole vector (or use swap ?).

Code that exercises type inference

一个人想着一个人 提交于 2019-12-05 04:54:13
问题 I'm working on an experimental programming language that has global polymorphic type inference. I recently got the algorithm working sufficiently well to correctly type the bits of sample code I'm throwing at it. I'm now looking for something more complex that will exercise the edge cases. Can anyone point me at a source of really gnarly and horrible code fragments that I can use for this? I'm sure the functional programming world has plenty. I'm particularly looking for examples that do evil

language without if's?

故事扮演 提交于 2019-12-05 03:00:16
A colleague said he heard of a language that did not have the concept of "if". Is that possible? If so, what language is it? Besides perhaps Prolog, I don't know of any specific languages, but I can think of a few ways a language without if statements may work. In fact, you don't need loop constructs either. You obviously needs some way of conditional branches and looping. If, for example, you had the following features: functions , ML-style pattern matching on function arguments and tail-call optimization , you could program without if's or loops. foo () { for (i = 1 to 10) { if even(i) {

Why does Rust check array bounds at runtime, when (most) other checks occur at compile time?

情到浓时终转凉″ 提交于 2019-12-05 02:47:49
Reading the basic introduction : If you try to use a subscript that is not in the array, you will get an error: array access is bounds-checked at run-time. Why does Rust check array bounds at runtime, when it seems most other checks occur at compile time? Because checking indices at compile time is not feasible in the general case. Reasoning about the possible values of arbitrary variables is somewhere between hard and impossible even for small programs. Nobody wants to have to: formally prove that the index can't be out of bounds, and encode that proof into the type system ... for every

Why is 'super' a keyword rather than a method in Ruby?

谁都会走 提交于 2019-12-05 02:10:11
In Ruby, super is a keyword rather than a method. Why was it designed this way? Ruby's design tends toward implementing as much as possible as methods; keywords are usually reserved for language features that have their own grammar rules. super , however, looks and acts like a method call. (I know it would be cumbersome to implement super in pure Ruby, since it would have to parse the method name out of caller , or use a trace_func . This alone wouldn't prevent it from being a method, because plenty of Kernel 's methods are not implemented in pure Ruby.) It behaves a little differently, in