language-features

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

﹥>﹥吖頭↗ 提交于 2019-12-07 14:41:15
问题 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

Is there a valid usecase for redefining “define” in scheme/racket?

依然范特西╮ 提交于 2019-12-07 07:47:59
问题 I'm playing around with racket/scheme and it allows me to redefine for instance define and bind it as a value. > (define define 2) > define 2 In that scope I can no longer define anything using define since it is obviously bound to 2. This works for all "keywords" I tried it with ( if , cond etc.). However it is not possible to use define to specify my own definition function: > (define mydef define) stdin::14: define: not allowed in an expression context in: define === context === /usr/share

How can I intercept execution of all the methods in a Java application using Groovy?

拟墨画扇 提交于 2019-12-07 04:58:53
问题 Is it possible to intercept all the methods called in a application? I'd like to do something with them, and then let them execute. I tried to override this behaviour in Object.metaClass.invokeMethod , but it doesn't seem to work. Is this doable? 回答1: Have you looked at Groovy AOP? There's very little documentation, but it allows you to define pointcuts and advice in a conceptually similar way as for AspectJ. Have a look at the unit tests for some more examples The example below will match

how can I create a truly immutable doubly linked list in C#?

夙愿已清 提交于 2019-12-07 01:50:46
问题 It is more of a theoretical question: Is it possible by any means in C# to create a truly immutable doubly linked list? A problem as I see it is in the mutual dependency of 2 adjacent nodes. By "truly" I mean using readonly fields. 回答1: This is possible to do with tricky constructor logic. For example public sealed class Node<T> { readonly T m_data; readonly Node<T> m_prev; readonly Node<T> m_next; // Data, Next, Prev accessors omitted for brevity public Node(T data, Node<T> prev, IEnumerator

C# - Indexer at its meaningful application

旧巷老猫 提交于 2019-12-07 00:07:39
问题 I understand indexer enable us to access a collection within a class as though the class itself were an array . Suppose we are developing a project ,where do we fit the real pratical usage (example may help) of such indexers ? 回答1: Look to almost any of the collection classes in the framework for an example. If I were to retrieve an item from a hashtable in a language without an indexer, I would use syntax like: object o = table.getvalue(key); where an indexer allows somewhat simpler syntax:

Why doesn't VB.NET 9 have Automatic Properties like C# 3?

痞子三分冷 提交于 2019-12-06 21:21:51
问题 Would having a nice little feature that makes it quicker to write code like Automatic Properties fit very nicely with the mantra of VB.NET? Something like this would work perfect: Public Property FirstName() As String Get Set End Property UPDATE: VB.NET 10 (coming with Visual Studio 2010 and .NET 4.0) will have Automatic Properties. Here's a link that shows a little info about the feature: http://geekswithblogs.net/DarrenFieldhouse/archive/2008/12/01/new-features-in-vb.net-10-.net-4.0.aspx In

A real use for `as` and `is`

烈酒焚心 提交于 2019-12-06 19:23:41
问题 I have -never- used as or is in C# or any language that supports the keyword. What have you used it for? I dont mean how do i use it i mean how have you actually need it? I also got away with doing no typecasting in a fairly large c++ project (i was proud). So considering i almost never typecast why do i need the keyword as or is ? 回答1: I had to write code to enumerate over all the controls placed on a ASP.NET webform and perform certain operations on specific controls, e.g. add a background

C#: enforceable way of signifying a method is there as part of interface

纵饮孤独 提交于 2019-12-06 08:15:08
Is there a way in C# to mark a method as being part of a class to satisfy an interface that the class implements? I find myself wondering sometimes when digging in a class's code why some methods are there but then, when I try to remove one since it isn't in use, I see it's necessary in order for the class to implement some interface. It'd be nice to have these methods marked as such. Something like the @Override annotation in Java, maybe. Edit: I would prefer to not explicitly implement the interface because then accessing the interface methods on an instance of my class becomes more of a

Is there any current proposal I can follow about adding proper locale support to JavaScript?

☆樱花仙子☆ 提交于 2019-12-06 03:08:30
Even though all major operating systems and programming languages/APIs have had locale support for at least a couple of decades, it seems that JavaScript still does not! With JavaScript becoming more and more of a full application programming language both in the browser and on the server, the lack of real locale support is becoming a real problem. Are there any proposals to add locale support as it exists in other languages to the EcmaScript standard? As part of the "harmony" project or anywhere else? I'd like to support and evangelize any such proposal. (This is a followup to my previous

is there an alternative way of calling next on python generators?

北战南征 提交于 2019-12-06 01:43:15
I have a generator and I would like to know if I can use it without having to worry about StopIteration , and I would like to use it without the for item in generator . I would like to use it with a while statement for example ( or other constructs ). How could I do that ? Use this to wrap your generator: class GeneratorWrap(object): def __init__(self, generator): self.generator = generator def __iter__(self): return self def next(self): for o in self.generator: return o raise StopIteration # If you don't care about the iterator protocol, remove this line and the __iter__ method. Use it like