language-design

Ruby Assignment Syntax

扶醉桌前 提交于 2019-12-04 01:18:57
问题 A silly, syntactical question: If the assignment operator is really a function, like def value=(x) @value = x end without a space between the left-hand operand and the "=", then why can the assignment be made as test.value = x (with a space), but the method definition cannot be written as: def value = (x) @value = x end with the space. Is this simply syntax dictated by the parser? 回答1: def needs to be followed by a token for the function name, optionally followed by an argument list. The

Why can't Regular Expressions use keywords instead of characters?

好久不见. 提交于 2019-12-04 01:18:21
Okay, I barely understand RegEx basics, but why couldn't they design it to use keywords (like SQL) instead of some cryptic wildcard characters and symbols? Is it for performance since the RegEx is interpreted/parsed at runtime? (not compiled) Or maybe for speed of writing? Considering that when you learn some "simple" character combinations it becomes easier to type 1 character instead of a keyword? Jeff Atwood You really want this ? Pattern findGamesPattern = Pattern.With.Literal(@"<div") .WhiteSpace.Repeat.ZeroOrMore .Literal(@"class=""game""").WhiteSpace.Repeat.ZeroOrMore.Literal(@"id=""")

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

假装没事ソ 提交于 2019-12-03 23:25:09
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' statement in C#, which I've pasted below: The following Visual Basic Select Case statement can't be

Why does c++ pointer * associate to the variable declared, not the type?

谁说我不能喝 提交于 2019-12-03 23:21:37
问题 Why was C++ designed such that the correct way to declare two int *s on the same line is int *x, *y; not int* x,y; I know some people think you should avoid either form and declare every variable on its own line, but I'm interested in why this language decision was made. 回答1: To keep compatibility with C code, because that's how C works. Bjarne makes a good point here: The choice between int* p; and int *p; is not about right and wrong, but about style and emphasis. C emphasized expressions;

Why must C/C++ string literal declarations be single-line?

岁酱吖の 提交于 2019-12-03 23:02:00
Is there any particular reason that multi-line string literals such as the following are not permitted in C++? string script = " Some Formatted String Literal "; I know that multi-line string literals may be created by putting a backslash before each newline. I am writing a programming language (similar to C) and would like to allow the easy creation of multi-line strings (as in the above example). Is there any technical reason for avoiding this kind of string literal? Otherwise I would have to use a python-like string literal with a triple quote (which I don't want to do): string script = """

Empty if-statements [duplicate]

故事扮演 提交于 2019-12-03 20:40:48
问题 This question already has answers here : Semicolon at end of 'if' statement (18 answers) Closed 6 years ago . By "empty if-statement", I mean something like this (note the semicolon): if (condition); I'm having trouble thinking of an application for this. With a while loop you can do this: while (callUntilReturnsFalse()); But there's no such application for an if-statement. What's more, the Java compiler doesn't issue an error or a warning when confronted with such a statement. This can lead

Code that exercises type inference

余生长醉 提交于 2019-12-03 20:31:28
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 things with function recursion, as I need to check to make sure that function expansion terminates

Why is there no implicit this in JavaScript

半城伤御伤魂 提交于 2019-12-03 19:12:51
问题 In JavaScript, this must always be stated explicitly when accessing its properties. For example: function Frobber(x) { this.x = x; return this; } Frobber.prototype.frob = function () { // wrong: return x * x; // right: return this.x * this.x; } I'm aware I can use with(this) (which is deprecated and generally frowned upon), but why aren't properties of this in scope automatically? I'm thinking there must be a reason for this design decision. 回答1: It's similar in Python. The reason is quite

Are there languages without “null”?

大兔子大兔子 提交于 2019-12-03 15:28:06
问题 There are many people who think that the concept of the special value null (as it is used in lanuages like C, Java, C#, Perl, Javascript, SQL etc.) is a bad idea. There are several questions about this on SO and P.SE, such as Best explanation for languages without null and Are null references really a bad thing? . However, I could not find any language that does without them. All the languages I'm familiar with have null , or something similar (e.g. "undefined" in Perl). I realize that

Why do Perl control statements require braces?

拜拜、爱过 提交于 2019-12-03 14:48:52
问题 This may look like the recent question that asked why Perl doesn't allow one-liners to be "unblocked," but I found the answers to that question unsatisfactory because they either referred to the syntax documentation that says that braces are required, which I think is just begging the question, or ignored the question and simply gave braceless alternatives. Why does Perl require braces for control statements like if and for ? Put another way, why does Perl require blocks rather than