language-features

Why is Self assignable in Delphi?

蓝咒 提交于 2019-11-30 17:17:52
This code in a GUI application compiles and runs: procedure TForm1.Button1Click(Sender: TObject); begin Self := TForm1.Create(Owner); end; (tested with Delphi 6 and 2009) why is Self writable and not read-only? in which situations could this be useful? Edit: is this also possible in Delphi Prism? (I think yes it is, see here ) Update: Delphi applications/libraries which make use of Self assignment: python4delphi That's not as bad as it could be. I just tested it in Delphi 2009, and it would seem that, while the Self parameter doesn't use const semantics, which you seem to be implying it should

How do you find the caller function? [duplicate]

时光毁灭记忆、已成空白 提交于 2019-11-30 15:15:32
Closed as exact duplicate of "How can I find the method that called the current method?" Is this possible with c#? void main() { Hello(); } void Hello() { // how do you find out the caller is function 'main'? } Console.WriteLine(new StackFrame(1).GetMethod().Name); However, this is not robust, especially as optimisations (such as JIT inlining) can monkey with the perceived stack frames. From here : System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1); System.Diagnostics.StackFrame sf = st.GetFrame(0); string msg = sf.GetMethod().DeclaringType.FullName + "." + sf.GetMethod()

Are move semantics incomplete?

◇◆丶佛笑我妖孽 提交于 2019-11-30 13:11:39
Move semantics replace copy semantics in situations where copying is inefficient. Copy semantics deals fully with copyable objects, including const objects. Already, there exists a myriad of non-copyable objects in c++11, for example std::unique_ptr. These objects rely on move semantics completely because moving from an object allows for invalidating it. This is important (imho) for popular design patterns like RAII. A problem occurs when a const non-copyable object is assigned to an area of memory. Such an object can't be recovered in any way. This is obviously important during the lifetime

How would you go about implementing off-side rule?

纵然是瞬间 提交于 2019-11-30 12:53:02
问题 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, I don't want to limit answers by language: token NEWLINE matches r"\n\ *" increase line count pick up and store the indentation level remember to also record

What is the rationale for not having static constructor in C++?

荒凉一梦 提交于 2019-11-30 11:22:56
问题 What is the rationale for not having static constructor in C++? If it were allowed, we would be initializing all the static members in it, at one place in a very organized way, as: //illegal C++ class sample { public: static int some_integer; static std::vector<std::string> strings; //illegal constructor! static sample() { some_integer = 100; strings.push_back("stack"); strings.push_back("overflow"); } }; In the absense of static constructor, it's very difficult to have static vector, and

Optional named arguments without wrapping them all in “OptionValue”

天大地大妈咪最大 提交于 2019-11-30 10:11:19
Suppose I have a function with optional named arguments but I insist on referring to the arguments by their unadorned names. Consider this function that adds its two named arguments, a and b: Options[f] = {a->0, b->0}; (* The default values. *) f[OptionsPattern[]] := OptionValue[a] + OptionValue[b] How can I write a version of that function where that last line is replaced with simply a+b ? (Imagine that that a+b is a whole slew of code.) The answers to the following question show how to abbreviate OptionValue (easier said than done) but not how to get rid of it altogether: Optional named

Why has Python decided against constant references?

泄露秘密 提交于 2019-11-30 08:02:42
Note: I'm not talking about preventing the rebinding of a variable. I'm talking about preventing the modification of the memory that the variable refers to, and of any memory that can be reached from there by following the nested containers. I have a large data structure, and I want to expose it to other modules, on a read-only basis. The only way to do that in Python is to deep-copy the particular pieces I'd like to expose - prohibitively expensive in my case. I am sure this is a very common problem, and it seems like a constant reference would be the perfect solution. But I must be missing

Shorthand for nested null checking C#

早过忘川 提交于 2019-11-30 07:58:09
问题 As far as I know there is not a significantly more elegant way to write the following.... string src; if((ParentContent!= null) &&(ParentContent.Image("thumbnail") != null) &&(ParentContent.Image("thumbnail").Property("src") != null)) src = ParentContent.Image("thumbnail").Property("src").Value Do you think there should be a C# language feature to make this shorter? And if so, what should it look like? for example, something like extending the ?? operator string src = ParentContent??.Image(

Is there, or is there ever going to be, a conditional operator in Delphi?

北城余情 提交于 2019-11-30 07:54:22
I kept my hands off Delphi for too long, I guess; busied myself with Java and PHP a lot over the last couple of years. Now, when I got back to doing a little Delphi job, I realised I really miss the conditional operator which is supported by both Java and PHP. On how many places would you find lines like these in your Delphi programs? var s : string; begin ...<here the string result is manipulated>... if combo.Text='' then s := 'null' else s := QuotedStr(combo.Text); result := result + s; end; where a simple result := result + (combo.text='')?'null':quotedStr(combo.text); would suffice. What I

Should inheritance (of non-interface types) be removed from programming languages?

百般思念 提交于 2019-11-30 06:48:18
This is quite a controversial topic, and before you say "no", is it really, really needed? I have been programming for about 10 years, and I can't honestly say that I can recall a time where inheritance solved a problem that couldn't be solved another way. On the other hand I can recall many times when I used inheritance, because I felt like I had to or because I though I was clever and ended up paying for it. I can't really see any circumstances where, from an implementation stand point, aggregation or another technique could not be used instead of inheritance. My only caveat to this is that