The ?. operator in languages besides CoffeeScript - coffeescript

I've seen the use of ?. operator in an article on CoffeeScript:
e.dataTransfer?.types?.contains 'Files'
which is equivalent to JavaScript's
e.dataTransfer && e.dataTransfer.types && e.dataTransfer.types.contains('Files')
Is there a name for such operator, and does it exist in other languages?

Sometimes it is called "Safe navigation" operator, it is presented in Groovy: http://groovy.codehaus.org/Operators#Operators-SafeNavigationOperator

It's called a safe navigation operator. Ruby has this as of 2.3.0: http://mitrev.net/ruby/2015/11/13/the-operator-in-ruby/
As mentioned in that article C# and Groovy also have this type of operator.

Related

Reactor 3.x: is there an operator for throttleLatest (conflate)?

In rx-java 2.x there is an operator named throttleLatest that conflates incoming events based on a given time:
https://github.com/ReactiveX/RxJava/pull/5979
Is there a similar operator in Reactor 3?
Or is it possible to get the same behaviour from combining other operators?
No, currently as of reactor-core 3.2.0/3.1.9 there is no such operator.

Why to use := in Scala? [duplicate]

What is the difference between = and := in Scala?
I have googled extensively for "scala colon-equals", but was unable to find anything definitive.
= in scala is the actual assignment operator -- it does a handful of specific things that for the most part you don't have control over, such as
Giving a val or var a value when it's created
Changing the value of a var
Changing the value of a field on a class
Making a type alias
Probably others
:= is not a built-in operator -- anyone can overload it and define it to mean whatever they like. The reason people like to use := is because it looks very assignmenty and is used as an assignment operator in other languages.
So, if you're trying to find out what := means in the particular library you're using... my advice is look through the Scaladocs (if they exist) for a method named :=.
from Martin Odersky:
Initially we had colon-equals for assignment—just as in Pascal, Modula, and Ada—and a single equals sign for equality. A lot of programming theorists would argue that that's the right way to do it. Assignment is not equality, and you should therefore use a different symbol for assignment. But then I tried it out with some people coming from Java. The reaction I got was, "Well, this looks like an interesting language. But why do you write colon-equals? What is it?" And I explained that its like that in Pascal. They said, "Now I understand, but I don't understand why you insist on doing that." Then I realized this is not something we wanted to insist on. We didn't want to say, "We have a better language because we write colon-equals instead of equals for assignment." It's a totally minor point, and people can get used to either approach. So we decided to not fight convention in these minor things, when there were other places where we did want to make a difference.
from The Goals of Scala's Design
= performs assignment. := is not defined in the standard library or the language specification. It's a name that is free for other libraries or your code to use, if you wish.
Scala allows for operator overloading, where you can define the behaviour of an operator just like you could write a method.
As in other languages, = is an assignment operator.
The is no standard operator I'm aware of called :=, but could define one with this name. If you see an operator like this, you should check up the documentation of whatever you're looking at, or search for where that operator is defined.
There is a lot you can do with Scala operators. You can essentially make an operator out of virtually any characters you like.

Why does CoffeeScript not support `==`?

I am learning CoffeeScript and am using js2.coffee to convert my Javascript file to CoffeeScript. One some lines, it warns me that
Operator '==' is not supported in CoffeeScript, use '===' instead.
What is the rationale behind that? Why does CoffeeScript not support ==?
For the same reason that most Linters warn you against using it.
It's a strangely implemented operator with surprising side-effects.
"\n\t" == false
// => true
There are many things written on the topic, but most notably the == operator made Douglas Crockford's list of things to avoid.
Actually CoffeeScript compiles == to JavaScript's === (and != to !== as you can see in the documentation).
So bottom line yes, it doesn't support it. I guess it is because == does type conversion before checking equality if the operands are of different type. This conversion is a practice that has been frowned upon because of its unexpected results and its performance.
There is much discussion online on the issue of == vs ===. MDN docs helped me get a better understanding on the issue.

Why do Scala Ints lack the postfix increment operator, but HashMaps don't?

Many data types in scala, like HashMaps, have the postfix increment operator implemented, but Ints and Doubles have not.
Is it because if there was a postfix operator for numbers, there had to be a prefix operator too, and that would not fit the way operators are implemented in scala (as methods)?
The ++ operator is not meant as an imperative increment operator, but as a union of two collection, such that it produces new collections instead of modifying either of the old ones.
The increment operator is a very imperatively styled thing. I think (!) as you're encouraged to program in a functional style, where variable values are not supposed to change in general, it is not present in Scala.
However, this is highly unofficially what I think from a Scala programmer point of view.

What is the difference between = and := in Scala?

What is the difference between = and := in Scala?
I have googled extensively for "scala colon-equals", but was unable to find anything definitive.
= in scala is the actual assignment operator -- it does a handful of specific things that for the most part you don't have control over, such as
Giving a val or var a value when it's created
Changing the value of a var
Changing the value of a field on a class
Making a type alias
Probably others
:= is not a built-in operator -- anyone can overload it and define it to mean whatever they like. The reason people like to use := is because it looks very assignmenty and is used as an assignment operator in other languages.
So, if you're trying to find out what := means in the particular library you're using... my advice is look through the Scaladocs (if they exist) for a method named :=.
from Martin Odersky:
Initially we had colon-equals for assignment—just as in Pascal, Modula, and Ada—and a single equals sign for equality. A lot of programming theorists would argue that that's the right way to do it. Assignment is not equality, and you should therefore use a different symbol for assignment. But then I tried it out with some people coming from Java. The reaction I got was, "Well, this looks like an interesting language. But why do you write colon-equals? What is it?" And I explained that its like that in Pascal. They said, "Now I understand, but I don't understand why you insist on doing that." Then I realized this is not something we wanted to insist on. We didn't want to say, "We have a better language because we write colon-equals instead of equals for assignment." It's a totally minor point, and people can get used to either approach. So we decided to not fight convention in these minor things, when there were other places where we did want to make a difference.
from The Goals of Scala's Design
= performs assignment. := is not defined in the standard library or the language specification. It's a name that is free for other libraries or your code to use, if you wish.
Scala allows for operator overloading, where you can define the behaviour of an operator just like you could write a method.
As in other languages, = is an assignment operator.
The is no standard operator I'm aware of called :=, but could define one with this name. If you see an operator like this, you should check up the documentation of whatever you're looking at, or search for where that operator is defined.
There is a lot you can do with Scala operators. You can essentially make an operator out of virtually any characters you like.