30
votes
Accepted
Why do heavily object-oriented languages avoid having functions as a primitive type?
IMO...
Because Java and C# are not true OO languages.
Functional programming was not in vogue when they were designed.
I agree with Jörg W Mittag, neither C# nor Java are true object-oriented ...
26
votes
Why do heavily object-oriented languages avoid having functions as a primitive type?
This is a little bit of a silly question. You're asking why object-oriented languages are object-oriented. If they passed functions around as first class types we wouldn't describe them as object-...
11
votes
Why do heavily object-oriented languages avoid having functions as a primitive type?
In these “classic OOP” languages like C++/Java/C#, objects are data + behaviour, where the behaviour is provided by a class. On a technical level, this generally results in a memory layout somewhat ...
6
votes
Accepted
Fundamental differences between a List<Action> vs Action
Yes, there is a fundamental difference between the two: type safety.
Whilst GetInvocationList can indeed be called on act in your example, it doesn't return a list, it returns an array of Delegate. ...
5
votes
Accepted
How to refactor same block of delegate code into a single private method?
Bundle the delegate and the local variable in a class:
class Validator
{
public string ActualValidationString { get; private set; } = string.Empty;
public void Validation(string msg)
{
...
5
votes
Are there any benefits to delegation over subclassing/inheritance in the case of a singleton?
Is there any benefit to using a delegate to specify behavior of a singleton rather than having the user override the singleton class and providing their own methods all in one place?
The singleton ...
5
votes
Resorted to Unconventional( i.e., hacky) "circuitous" programming code techniques when using populating a list of C# Action Delegates via a for loop
This isn't how you would iterate a dictionary. For one, it does a linear walk through its keys/values on every call to .Keys.ElementAt(i), which is really wasteful. More importantly, it's just more ...
4
votes
Why do heavily object-oriented languages avoid having functions as a primitive type?
The use of "primitive" is a bit too broad in the question.
In general terms, primitives are things that have simple, measurable, binary storage, without iteration or growth in allocated ...
4
votes
Why do heavily object-oriented languages avoid having functions as a primitive type?
In later C# versions you no longer have to be explicit about delegates. You can pass a function just by stating its name.
I think it is mainly about type safety. In the compiled code there won't be an ...
2
votes
Are there any benefits to delegation over subclassing/inheritance in the case of a singleton?
Nobody cares about whatever table size.
But if you create a subclass, then a developer who knows the behaviour of the original class has no clue what your subclass will be doing. A macOS / iOS ...
2
votes
Functions vs Classes for delegate pattern
Using a lambda function of type std::function<void()> or an object which is derived from an abstract class with just one virtual function are both semantically equivalent. More general, objects ...
2
votes
Why do heavily object-oriented languages avoid having functions as a primitive type?
Without delving into any specific language (yet!), one first reason would probably be ease of implementation (in the compiler/interpreter). It is much much more simple to have a single "internal&...
1
vote
Turn an asynchronous delegate pattern into a blocking method
Imagine if someone else calls doWork for that same object right after you do. They "steal" the delegate, and they will get both workDone callbacks
basically, this is what you should be fixing first. ...
1
vote
Design issue with delegation, inheritance and dependency injection
It seems that MailService should only have one MailerServiceDelegate during it's lifetime. I am not sure if you can change MailService, but I think that it's current implementation have some flaws.
...
1
vote
Fundamental differences between a List<Action> vs Action
The main difference is internal. Delegate is a linked list; List uses an array internally. For a public API, you should not use either. List is not supposed to be exposed in properties, and a public ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
delegates × 65c# × 21
design-patterns × 16
java × 7
.net × 7
design × 4
architecture × 3
object-oriented × 3
interfaces × 3
inheritance × 3
ios × 3
scala × 3
composition × 3
mvc × 2
multithreading × 2
dependency-injection × 2
language-design × 2
functions × 2
objective-c × 2
singleton × 2
swift-language × 2
trait × 2
subclassing × 2
c++ × 1
python × 1