language-construct

Why would I use java.lang.Class.cast [duplicate]

我怕爱的太早我们不能终老 提交于 2019-12-01 14:59:10
This question already has an answer here: When should I use the java 5 method cast of Class? 5 answers Java Class.cast() vs. cast operator 8 answers I recently stumbled upon a piece of code that went like this: Object o = .. ; Foo foo = Foo.class.cast(o); I was actually not even aware that java.lang.Class had a cast method, so I looked into the docs, and from what I gather this does simply do a cast to the class that the Class object represents. So the code above would be roughly equivalent to Object o = ..; Foo foo = (Foo)o; So I wondered, why I would want to use the cast method instead of

Questions on a Haskell -> C# conversion

こ雲淡風輕ζ 提交于 2019-11-30 14:46:22
问题 Background: I was "dragged" into seeing this question: Fibonacci's Closed-form expression in Haskell when the author initially tagged with many other languages but later focused to a Haskell question. Unfortunately I have no experience whatsoever with Haskell so I couldn't really participate in the question. However one of the answers caught my eye where the answerer turned it into a pure integer math problem. That sounded awesome to me so I had to figure out how it worked and compare this to

Questions on a Haskell -> C# conversion

不想你离开。 提交于 2019-11-30 11:36:46
Background: I was "dragged" into seeing this question: Fibonacci's Closed-form expression in Haskell when the author initially tagged with many other languages but later focused to a Haskell question. Unfortunately I have no experience whatsoever with Haskell so I couldn't really participate in the question. However one of the answers caught my eye where the answerer turned it into a pure integer math problem. That sounded awesome to me so I had to figure out how it worked and compare this to a recursive Fibonacci implementation to see how accurate it was. I have a feeling that if I just

How can I emulate destructuring in C++?

ぐ巨炮叔叔 提交于 2019-11-30 07:52:40
In JavaScript ES6, there is a language feature known as destructuring . It exists across many other languages as well. In JavaScript ES6, it looks like this: var animal = { species: 'dog', weight: 23, sound: 'woof' } //Destructuring var {species, sound} = animal //The dog says woof! console.log('The ' + species + ' says ' + sound + '!') What can I do in C++ to get a similar syntax and emulate this kind of functionality? For the specific case of std::tuple (or std::pair ) objects, C++ offers the std::tie function which looks similar: std::tuple<int, bool, double> my_obj {1, false, 2.0}; //

How can I emulate destructuring in C++?

家住魔仙堡 提交于 2019-11-29 10:39:58
问题 In JavaScript ES6, there is a language feature known as destructuring. It exists across many other languages as well. In JavaScript ES6, it looks like this: var animal = { species: 'dog', weight: 23, sound: 'woof' } //Destructuring var {species, sound} = animal //The dog says woof! console.log('The ' + species + ' says ' + sound + '!') What can I do in C++ to get a similar syntax and emulate this kind of functionality? 回答1: For the specific case of std::tuple (or std::pair ) objects, C++

Use constant as class name

北战南征 提交于 2019-11-28 07:49:59
问题 I need to use constant as class name for acces to this class static property, that is class a { public static $name = "Jon"; } define("CLASSNAME", "a"); echo CLASSNAME::$name; this returns error, that class CLASSNAME not exists. There is some solution ? 回答1: It's possible with reflection: class a { public static $name = "Jon"; } define("CLASSNAME", "a"); $obj = new ReflectionClass(CLASSNAME); echo $obj->getStaticPropertyValue("name"); If it is a good design choice is another question... 回答2:

Why does Scala support shadow variables? [closed]

谁说我不能喝 提交于 2019-11-27 09:09:01
I think that shadow variables are too dangerous to use them. Why does Scala support this language construct? There should be some strong reason for that, but I cant find it. Just as a reminder: A variable, method, or type is said to shadow another variable, method, or type of the same name when it is declared in an inner scope, making it impossible to refer to the outer-scope entity in an unqualified way (or, sometimes, at all). Scala, just like Java, allows shadowing. One possible reason I could see is that in Scala, it is frequent to have many nested scopes, each of which is relatively short

Specifying default parameter when calling C++ function

拈花ヽ惹草 提交于 2019-11-27 02:40:53
问题 Suppose I have code like this: void f(int a = 0, int b = 0, int c = 0) { //...Some Code... } As you can evidently see above with my code, the parameters a , b , and c have default parameter values of 0. Now take a look at my main function below: int main() { //Here are 4 ways of calling the above function: int a = 2; int b = 3; int c = -1; f(a, b, c); f(a, b); f(a); f(); //note the above parameters could be changed for the other variables //as well. } Now I know that I can't just skip a

What is ?: in PHP 5.3? [duplicate]

99封情书 提交于 2019-11-26 17:23:11
Possible Duplicate: What are the PHP operators “?” and “:” called and what do they do? From http://twitto.org/ <?PHP require __DIR__.'/c.php'; if (!is_callable($c = @$_GET['c'] ?: function() { echo 'Woah!'; })) throw new Exception('Error'); $c(); ?> Twitto uses several new features available as of PHP 5.3: The DIR constant The ?: operator Anonymous functions What does number 2 do with the ?: in PHP 5.3? Also, what do they mean by anonymous functions? Wasn't that something that has existed for a while? Ben James ?: is a form of the conditional operator which was previously available only as:

Is there a way to implement custom language features in C#?

谁都会走 提交于 2019-11-26 17:19:48
I've been puzzling about this for a while and I've looked around a bit, unable to find any discussion about the subject. Lets assume I wanted to implement a trivial example, like a new looping construct: do..until Written very similarly to do..while do { //Things happen here } until (i == 15) This could be transformed into valid csharp by doing so: do { //Things happen here } while (!(i == 15)) This is obviously a simple example, but is there any way to add something of this nature? Ideally as a Visual Studio extension to enable syntax highlighting etc. Microsoft proposes Rolsyn API as an