Java 9 - List class: of() overloaded methods with varargs [duplicate] - java-9

This question already has answers here:
What is the point of overloaded Convenience Factory Methods for Collections in Java 9
(6 answers)
Java 9, Set.of() and Map.of() varargs overloads [duplicate]
(3 answers)
Closed 4 years ago.
In Java 9, under the List interface, there is a new method of() which according to Java documentation:
The List.of() static factory methods provide a convenient way to
create immutable lists.
They have overloaded of() with up to 10 arguments together with one vararg argument.
What can be the rationale behind overloading of() with 10 arguments when they have provided one vararg argument overloaded method as well?

This is done for performance reasons.
First, empty, one element and two element lists are implemented as dedicated classes, which have no nested objects. So, constructing those is the fastest (only one allocation involved).
As for the higher "arities", the interface designers probably wanted to keep their options open to possibly implement more "fixed arity" list classes (though they had not followed this route just yet).
So indeed, the "higher arity" overloads get wrapped back into the "varargs" list constructor.
Reference: https://github.com/dmlloyd/openjdk/blob/7d7fbd09fcfd7f8cd02bf76ce10433ceeb33b3cf/jdk/src/java.base/share/classes/java/util/List.java#L788

Related

What's the difference between generic and non-generic use of interfaces? [duplicate]

This question already has answers here:
What are the benefits of replacing an interface argument with a type parameter?
(2 answers)
Closed 8 months ago.
In the new Type Parameters Design Draft, you can now apply an interface constraint to a generic function:
func prettyPrint[T Stringer](s T) string {
...
}
But, this was already possible by using an interface parameter without generics:
func prettyPrint(s Stringer) string {
...
}
What's the difference between using the first and using the second?
I assume the question refers to the latest draft of the Type Parameters proposal, which may end up in Go in 1.18.
The first is parametric polymorphism. The compiler verifies that the constraint is satisfied, and then generates code that takes a statically-known type. Importantly, it's not boxed.
The second is runtime polymorphism. It takes a type that's unknown at compile time (the only thing known is that it implements the interface) and works on a boxed interface pointer.
Performance considerations aside, in this simple case you can use either approach. Generics really help with the more complicated cases where the current tools don't work well.

Dart v1.8: new feature enum [duplicate]

This question already has answers here:
How can I build an enum with Dart? [duplicate]
(4 answers)
Closed 8 years ago.
A simple question here :) I'm really happy to see a new feature in the dart language. But I just realized that I kind of never use enumeration.I don't know if there is a discussion somewhere about that but.
What is the pros and cons of this feature in terms of code writing (seems shorter), performance,etc?
Cheers
If I understand it correctly an enum is like a class with const members and some useful methods. Given that cost members are resolved by the compiler using an enum should not incur in any performance hit.
In terms of "code writing" enums are good candidates to replace classic const or static enumerations or, even better, hard-coded constants.

Defining custom operators in ruby [duplicate]

This question already has answers here:
Define custom Ruby operator
(3 answers)
Closed 9 years ago.
I would like to be able to define custom operators. Is that possible? For example, to make a***b mean something.
Is it also possible to monkey patch existing operators? To, for example, make a**b always return a float?
Yes, you can. For example:
class Fixnum
def **(x)
self.*(x)*1.0
end
end
5**4 #==> 20.0
Custom operators? Not unless you want to hack the C parser (or Java parser for JRuby or ...). OTOH, operators are mostly syntactic sugar for methods and you're allowed to defined all the methods you want.
Since many operators (but not all) are just methods in disguise, you can monkey patch the implementations of existing operators as much as you want to. You'd have to track down all the numeric classes that define their own ** implementation and patch all of them; note that you'd need to cover Rational, Complex, ... from the core as well as things like BigDecimal from the standard library. I'd strongly recommend against doing this, you'd just be setting yourself up for pain and suffering; for example, what would you do with BigDecimal#** when the result doesn't fit in a Float? What about Complex#**? If you need Floats for something, make it explicit with a to_f call.

Why is the new method not needed for creating Rational in ruby [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Ruby syntax question: Rational(a, b) and Rational.new!(a, b)
I'm in the process of reading the ruby pickaxe book, and I'm confused about the syntax of creating rational numbers.
Rational(3,4) * Rational(1,2)
produces
=> 3/8
Why is the new method not needed for Rational (I also noticed for example I can create a string without the new method)?
For one thing, Ruby has no new keyword. new is a class method that all classes have (they inherit it from Class) that creates an object of that class. When you see something like Rational(3,4), Rational is really just a private method of Object (defined in Kernel) that makes creating rational numbers easier. For more on those constructor-methods, see this answer of mine: https://stackoverflow.com/a/9677125/1008938
It's a method that happens to have the same name as the class. It's a common conversion idiom in Ruby.

Perfect forwarding - what's it all about? [duplicate]

This question already has answers here:
Closed 10 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Possible Duplicate:
Advantages of using forward
Could someone please explain to me what perfect forwarding is about?
http://www.justsoftwaresolutions.co.uk/cplusplus/rvalue_references_and_perfect_forwarding.html
Why is this useful? Well, it means that a function template can pass its arguments through to another function whilst retaining the lvalue/rvalue nature of the function arguments by using std::forward. This is called "perfect forwarding", avoids excessive copying, and avoids the template author having to write multiple overloads for lvalue and rvalue references.
Quoting Session Announcement: Adventures in Perfect Forwarding:
Perfecting forwarding is an important C++0x technique built atop
rvalue references. It allows move semantics to be automatically
applied, even when the source and the destination of a move are
separated by intervening function calls. Common examples include
constructors and setter functions that forward arguments they receive
to the data members of the class they are initializing or setting, as
well as standard library functions like make_shared, which
“perfect-forwards” its arguments to the class constructor of whatever
object the to-be-created shared_ptr is to point to.

Resources