Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Method reference operator(double colon) equivalent in typescript

Tags:

What is the equivalent of Java double colon operator in typescript(if there's any):

stream.forEach( System.out::println(s));

EDIT: I know it's doable with certain functions like:

stream.forEach(console.log);
stream.filter(Boolean);

But when I use other functions e.g. BehaviorSubject "next" my code breaks. I don't know what qualifies those other two to be called by reference. I'd like to have something like:

stream.pipe(someSubject.next);

Instead of:

stream.pipe(value => someSubject.next(value));
like image 508
Wildhammer Avatar asked May 17 '19 18:05

Wildhammer


People also ask

What does double colon mean in typescript?

Double colon ( :: ) is used as a seperator for nested JSON arrays.

What is double colon in Javascript?

The Bind Operator :: # The Bind operator consists of an introduction of a new operator :: (double colon), which acts as syntax sugar for the previous two use cases.

What is double colon operator in Java?

The double colon (::) operator, also known as method reference operator in Java, is used to call a method by referring to it with the help of its class directly. They behave exactly as the lambda expressions.

What is the meaning of :: in Java 8?

:: is a new operator included in Java 8 that is used to refer to a method of an existing class. You can refer to static methods and non-static methods of a class. For referring to static methods, the syntax is: ClassName :: methodName.


1 Answers

Okay, so I did a bit of searching and there is a double colon operator in JScript, which was Microsoft's implementation of JavaScript:

a double colon is used as separator between the script ID and the event name

My guess is that's not part (or no longer part) of Internet explorer's ECMAScript implementation but it belongs (or used to belong) to Microsoft Office's implementation

What does ‘::’ (double colon) do in javascript for events?

Which was a really cool find, but I've never seen a double colon operator being used in TS or ECMAScript. You can use lambda expressions [].forEach((item: any) => console.log(item))

but I'm pretty sure JS or TS doesn't have a wrapper for a lambda expression like Java does.

EDIT: I also found this What does ‘::’ (double colon) do in JavaScript? after a little more searching and it is also a valid ES7 operator as syntactic sugar for bind: http://blog.jeremyfairbank.com/javascript/javascript-es7-function-bind-syntax/

Although it doesn't behave the same way as Java's :: operator.

like image 109
Marc Freeman Avatar answered Oct 12 '22 03:10

Marc Freeman