language-features

Why isn't the eigenclass equivalent to self.class, when it looks so similar?

醉酒当歌 提交于 2019-11-26 21:15:39
I've missed the memo somewhere, and I hope you'll explain this to me. Why is the eigenclass of an object different from self.class ? class Foo def initialize(symbol) eigenclass = class << self self end eigenclass.class_eval do attr_accessor symbol end end end My train of logic that equates the eigenclass with class.self is rather simple: class << self is a way of declaring class methods, rather than instance methods. It's a shortcut to def Foo.bar . So within the reference to the class object, returning self should be identical to self.class . This is because class << self would set self to

How to hide (remove) a base class's methods in C#? [duplicate]

∥☆過路亽.° 提交于 2019-11-26 20:12:57
问题 This question already has answers here : C# - Can publicly inherited methods be hidden (e.g. made private to derived class) (10 answers) Closed 4 years ago . The essence of the problem is, given a class hierarchy like this: class A { protected void MethodToExpose() {} protected void MethodToHide(object param) {} } class B : A { new private void MethodToHide(object param) {} protected void NewMethodInB() {} } class C : B { public void DoSomething() { base.MethodToHide("the parameter"); // This

Methods in Ruby: objects or not?

家住魔仙堡 提交于 2019-11-26 18:35:59
Inspired by this discussion , after some googling I wasn't able to find an answer to a pretty simple question regarding methods in Ruby: are methods objects or not? There are different opinions here and there , and I would really like to hear, let's say, an in-depth explanation. I'm aware of Object#method method, which takes a method name and returns a Method instance, but, on the other hand, there's a similar thing you can do with blocks to make them into Proc instances, and blocks aren't objects, so what makes methods any different? JRL Methods are a fundamental part of Ruby's syntax, but

Javascript as a functional language

感情迁移 提交于 2019-11-26 17:57:50
问题 I am looking get to grips with functional programming concepts. I've used Javascript for many years for client side scripting in web applications and apart from using prototypes it was all simple DOM manipulation, input validation etc. Of late, I have often read that Javascript is one of the languages that supports functional programming. With my familiarity and experience with Javascript, my preference is to use it to learn functional programming. I expect I would be able to concentrate more

What's the difference between interface and @interface in java?

为君一笑 提交于 2019-11-26 17:54:39
问题 I haven't touched Java since using JBuilder in the late 90's while at University, so I'm a little out of touch - at any rate I've been working on a small Java project this week, and using Intellij IDEA as my IDE, for a change of pace from my regular .Net development. I notice it has support for adding interfaces and @interfaces, what is an @interface, and how does it differ from a normal interface? public interface Test { } vs. public @interface Test { } I've done a bit of searching, but

C#: No implict conversion from Class<Child> to Class<Base>

烈酒焚心 提交于 2019-11-26 17:07:33
问题 Following snippet wouldn't compile. With following error: Cannot implicitly convert type 'Container<ChildClass>' to 'Container<BaseClass>' class BaseClass {} class ChildClass : BaseClass {} class Container<T> where T : BaseClass {} class Program { static void Main() { // why doesn't this work? Container<BaseClass> obj = new Container<ChildClass>(); } } Is this by design? If it is, what is the reason? 回答1: (made wiki, in case of dups) C# (3.0) doesn't support covariance of lists etc. C# 4.0

Why is there a `null` value in JavaScript?

核能气质少年 提交于 2019-11-26 16:56:44
In JavaScript, there are two values which basically say 'I don't exist' - undefined and null . A property to which a programmer has not assigned anything will be undefined , but in order for a property to become null , null must be explicitly assigned to it. I once thought that there was a need for null because undefined is a primitive value and null an object. It's not, even if typeof null will yield 'object' : Actually, both are primitive values - which means neither undefined nor null can be returned from a constructor function, as both will be converted to an empty object (one has to throw

Double dispatch in C#?

ⅰ亾dé卋堺 提交于 2019-11-26 15:51:59
I have heard/read the term but don't quite understand what it means. When should I use this technique and how would I use it? Can anyone provide a good code sample? The visitor pattern is a way of doing double-dispatch in an object-oriented way. It's useful for when you want to choose which method to use for a given argument based on its type at runtime rather than compile time. Double dispatch is a special case of multiple dispatch . When you call a virtual method on an object, that's considered single-dispatch because which actual method is called depends on the type of the single object.

What&#39;s the difference between a hash and hash reference in Perl?

馋奶兔 提交于 2019-11-26 15:49:02
I would like to properly understand hashes in Perl. I've had to use Perl intermittently for quite some time and mostly whenever I need to do it, it's mostly related to text processing. And everytime, I have to deal with hashes, it gets messed up. I find the syntax very cryptic for hashes A good explanation of hashes and hash references, their differences, when they are required etc. would be much appreciated. A simple hash is close to an array. Their initializations even look similar. First the array: @last_name = ( "Ward", "Cleaver", "Fred", "Flintstone", "Archie", "Bunker" ); Now let's

Why are private fields private to the type, not the instance?

泄露秘密 提交于 2019-11-26 15:45:35
In C# (and many other languages) it's perfectly legitimate to access private fields of other instances of the same type. For example: public class Foo { private bool aBool; public void DoBar(Foo anotherFoo) { if (anotherFoo.aBool) ... } } As the C# specification (sections 3.5.1, 3.5.2) states access to private fields is on a type, not an instance. I've been discussing this with a colleague and we're trying to come up with a reason why it works like this (rather than restricting access to the same instance). The best argument we could come up with is for equality checks where the class may want