Skip to main content
added 8 characters in body
Source Link
Zev Spitz
  • 693
  • 1
  • 5
  • 19

Some languages (Javascript, Python) have the notion that a function is an object:

//Javascript
var fn = console.log;
//Javascript
var fn = console.log;

This means that functions can be treated like any other object (first-class functions), e.g. passed in as an argument to another function:

var invoker = function(toInvoke) {
    toInvoke();
};
invoker(fn); //will call console.log
var invoker = function(toInvoke) {
    toInvoke();
};
invoker(fn); //will call console.log

Other languages (C++, C#, VB.NET) do not define functions as real objects:

//C#
Type t = Console.WriteLine.GetType();
//This code will not compile, because:
//"'Console.WriteLine()' is a method, which is not valid in the given context"
//C#
Type t = Console.WriteLine.GetType();
//This code will not compile, because:
//"'Console.WriteLine()' is a method, which is not valid in the given context"

Rather these languages may have objects which can point to a function (such as C++ function pointers) and can be passed around just like any other object. In the CLI, these wrapper objects are called delegates or delegate instances:

//C#
void Invoker(Action toInvoke) {
    toInvoke();
}

Action action = Console.WriteLine;
Invoker(action);

//also valid, and the toInvoke argument will now contain a delegate which points to Console.WriteLine
//Invoker(Console.WriteLine);
//C#
void Invoker(Action toInvoke) {
    toInvoke();
}

Action action = Console.WriteLine;
Invoker(action);

//also valid, and the toInvoke argument will now contain a delegate which points to Console.WriteLine
//Invoker(Console.WriteLine);

What differences in capability arise from these two mechanisms -- "function object" vs "pointer-to-function as object"?

Some languages (Javascript, Python) have the notion that a function is an object:

//Javascript
var fn = console.log;

This means that functions can be treated like any other object (first-class functions), e.g. passed in as an argument to another function:

var invoker = function(toInvoke) {
    toInvoke();
};
invoker(fn); //will call console.log

Other languages (C++, C#, VB.NET) do not define functions as real objects:

//C#
Type t = Console.WriteLine.GetType();
//This code will not compile, because:
//"'Console.WriteLine()' is a method, which is not valid in the given context"

Rather these languages may have objects which can point to a function (such as C++ function pointers) and can be passed around just like any other object. In the CLI, these wrapper objects are called delegates or delegate instances:

//C#
void Invoker(Action toInvoke) {
    toInvoke();
}

Action action = Console.WriteLine;
Invoker(action);

//also valid, and the toInvoke argument will now contain a delegate which points to Console.WriteLine
//Invoker(Console.WriteLine);

What differences in capability arise from these two mechanisms -- "function object" vs "pointer-to-function as object"?

Some languages (Javascript, Python) have the notion that a function is an object:

//Javascript
var fn = console.log;

This means that functions can be treated like any other object (first-class functions), e.g. passed in as an argument to another function:

var invoker = function(toInvoke) {
    toInvoke();
};
invoker(fn); //will call console.log

Other languages (C++, C#, VB.NET) do not define functions as real objects:

//C#
Type t = Console.WriteLine.GetType();
//This code will not compile, because:
//"'Console.WriteLine()' is a method, which is not valid in the given context"

Rather these languages may have objects which can point to a function (such as C++ function pointers) and can be passed around just like any other object. In the CLI, these wrapper objects are called delegates or delegate instances:

//C#
void Invoker(Action toInvoke) {
    toInvoke();
}

Action action = Console.WriteLine;
Invoker(action);

//also valid, and the toInvoke argument will now contain a delegate which points to Console.WriteLine
//Invoker(Console.WriteLine);

What differences in capability arise from these two mechanisms -- "function object" vs "pointer-to-function as object"?

added 14 characters in body
Source Link
Zev Spitz
  • 693
  • 1
  • 5
  • 19

Some languages (Javascript, Python) have the notion that a function is an object:

//Javascript
var fn = console.log;

This means that functions can be treated like any other object (first-class functions), e.g. passed in as an argument to another function:

var invoker = function(toInvoke) {
    toInvoke();
};
invoker(fn); //will call console.log
 

Other languages (C++, C#, VB.NET) do not define functions as real objects:

//C#
Type t = Console.WriteLine.GetType();
//This code will not compile, because:
//"'Console.WriteLine()' is a method, which is not valid in the given context"

Rather these languages may have objects which can point to a function (such as C++ function pointers) and can be passed around just like any other object. In the CLI, these wrapper objects are called delegates or delegate instances:

//C#
void Invoker(Action toInvoke) {
    toInvoke();
}

Action action = Console.WriteLine;
Invoker(action);

//also valid, and the toInvoke argument will now contain a delegate which points to Console.WriteLine
//Invoker(Console.WriteLine);
 

What differences in capability arise from these two mechanisms -- "function object" vs "pointer-to-function as object"?

Some languages (Javascript, Python) have the notion that a function is an object:

//Javascript
var fn = console.log;

This means that functions can be treated like any other object (first-class functions), e.g. passed in as an argument to another function:

var invoker = function(toInvoke) {
    toInvoke();
};
invoker(fn); //will call console.log

Other languages (C++, C#, VB.NET) do not define functions as real objects:

//C#
Type t = Console.WriteLine.GetType();
//This code will not compile, because:
//"'Console.WriteLine()' is a method, which is not valid in the given context"

Rather these languages may have objects which can point to a function (such as C++ function pointers) and can be passed around just like any other object. In the CLI, these wrapper objects are called delegates or delegate instances:

//C#
void Invoker(Action toInvoke) {
    toInvoke();
}

Action action = Console.WriteLine;
Invoker(action);

//also valid, and the toInvoke argument will now contain a delegate which points to Console.WriteLine
//Invoker(Console.WriteLine);

What differences in capability arise from these two mechanisms -- "function object" vs "pointer-to-function as object"?

Some languages (Javascript, Python) have the notion that a function is an object:

//Javascript
var fn = console.log;

This means that functions can be treated like any other object (first-class functions), e.g. passed in as an argument to another function:

var invoker = function(toInvoke) {
    toInvoke();
};
invoker(fn); //will call console.log
 

Other languages (C++, C#, VB.NET) do not define functions as real objects:

//C#
Type t = Console.WriteLine.GetType();
//This code will not compile, because:
//"'Console.WriteLine()' is a method, which is not valid in the given context"

Rather these languages may have objects which can point to a function (such as C++ function pointers) and can be passed around just like any other object. In the CLI, these wrapper objects are called delegates or delegate instances:

//C#
void Invoker(Action toInvoke) {
    toInvoke();
}

Action action = Console.WriteLine;
Invoker(action);

//also valid, and the toInvoke argument will now contain a delegate which points to Console.WriteLine
//Invoker(Console.WriteLine);
 

What differences in capability arise from these two mechanisms -- "function object" vs "pointer-to-function as object"?

spelling
Source Link
Zev Spitz
  • 693
  • 1
  • 5
  • 19

Some languages (Javascript, Python) have the notion that a function is an object:

//Javascript
var fn = console.log;

This means that functions can be treated like any other object (first-class functions), e.g. passed in as argumentsan argument to another function:

var invoker = function(toInvoke) {
    toInvoke();
};
invoker(fn); //will call console.log

Other languages (C++, C#, VB.NET) do not define functions as real objects:

//C#
varType t = Console.WriteLine.GetType();
//This code will not compile, because:
//"'Console.WriteLine()' is a method, which is not valid in the given context"

Rather these languages may have objects which can point to a function (such as C++ function pointers) and can be passed around just like any other object. In the CLI, these wrapper objects are called delegates or delegate instances:

//C#
void Invoker(Action toInvoke) {
    toInvoke();
}

Action action = Console.WriteLine;
Invoker(action);

//also valid, and the toIvoketoInvoke argument will now contain a delegate which points to Console.WriteLine
//Invoker(Console.WriteLine);

What differences in capability arise from these two mechanisms -- "function object" vs "pointer-to-function as object"?

Some languages (Javascript, Python) have the notion that a function is an object:

//Javascript
var fn = console.log;

This means that functions can be treated like any other object (first-class functions), e.g. passed in as arguments to another function:

var invoker = function(toInvoke) {
    toInvoke();
};
invoker(fn); //will call console.log

Other languages (C++, C#, VB.NET) do not define functions as real objects:

//C#
var t = Console.WriteLine.GetType();
//This code will not compile, because:
//"'Console.WriteLine()' is a method, which is not valid in the given context"

Rather these languages may have objects which can point to a function (such as C++ function pointers) and can be passed around just like any other object. In the CLI, these wrapper objects are called delegates or delegate instances:

//C#
void Invoker(Action toInvoke) {
    toInvoke();
}

Action action = Console.WriteLine;
Invoker(action);

//also valid, and the toIvoke argument will now contain a delegate which points to Console.WriteLine
//Invoker(Console.WriteLine);

What differences in capability arise from these two mechanisms -- "function object" vs "pointer-to-function as object"?

Some languages (Javascript, Python) have the notion that a function is an object:

//Javascript
var fn = console.log;

This means that functions can be treated like any other object (first-class functions), e.g. passed in as an argument to another function:

var invoker = function(toInvoke) {
    toInvoke();
};
invoker(fn); //will call console.log

Other languages (C++, C#, VB.NET) do not define functions as real objects:

//C#
Type t = Console.WriteLine.GetType();
//This code will not compile, because:
//"'Console.WriteLine()' is a method, which is not valid in the given context"

Rather these languages may have objects which can point to a function (such as C++ function pointers) and can be passed around just like any other object. In the CLI, these wrapper objects are called delegates or delegate instances:

//C#
void Invoker(Action toInvoke) {
    toInvoke();
}

Action action = Console.WriteLine;
Invoker(action);

//also valid, and the toInvoke argument will now contain a delegate which points to Console.WriteLine
//Invoker(Console.WriteLine);

What differences in capability arise from these two mechanisms -- "function object" vs "pointer-to-function as object"?

modified title
Source Link
Zev Spitz
  • 693
  • 1
  • 5
  • 19
Loading
Added example of trying to use Console.WriteLine as an object
Source Link
Zev Spitz
  • 693
  • 1
  • 5
  • 19
Loading
deleted 17 characters in body; edited tags
Source Link
Zev Spitz
  • 693
  • 1
  • 5
  • 19
Loading
deleted 9 characters in body
Source Link
Zev Spitz
  • 693
  • 1
  • 5
  • 19
Loading
added 9 characters in body
Source Link
Zev Spitz
  • 693
  • 1
  • 5
  • 19
Loading
added 49 characters in body
Source Link
Zev Spitz
  • 693
  • 1
  • 5
  • 19
Loading
added 145 characters in body
Source Link
Zev Spitz
  • 693
  • 1
  • 5
  • 19
Loading
Changed samples to use static methods instead of instance methods; clarified question
Source Link
Zev Spitz
  • 693
  • 1
  • 5
  • 19
Loading
Tweeted twitter.com/StackProgrammer/status/714385847508996097
added 4 characters in body
Source Link
Zev Spitz
  • 693
  • 1
  • 5
  • 19
Loading
Updated tag
Link
amon
  • 135.1k
  • 27
  • 292
  • 384
Loading
deleted 134 characters in body
Source Link
Zev Spitz
  • 693
  • 1
  • 5
  • 19
Loading
edited title
Source Link
Zev Spitz
  • 693
  • 1
  • 5
  • 19
Loading
Source Link
Zev Spitz
  • 693
  • 1
  • 5
  • 19
Loading