https://perlmaven.com/wantarray
That always appeared as madness to me. Why would you make things that cryptic to understand?
https://perlmaven.com/wantarray
That always appeared as madness to me. Why would you make things that cryptic to understand?
You can do that in Java, if I understand that feature correctly.
For example, it's possible to write a method that returns a generic type over whatever type you're trying to assign to, e.g.:
static <T> List<T> listOfAnything() { return (List<T>) new ArrayList(); }
Which can be used like this: List<String> stringList = listOfAnything();
Or by letting the compiler infer the type from the return type of a method: List<Integer> listOfInts() { return listOfAnything(); }
This is not just some esoteric feature... it's quite commonly used by, for example, deserialization libraries and functional programming.Wantarray is not a generic implementation. It's closer to reflection. You can detect the type of variable the caller is putting your functions result in. Then you can branch your program based on the variable type that the caller wants. I guess this is similar to type inference but not exactly as it's not part of the type system. For example, if they want an array return the amounts of each transaction in an array if not return the sum of the transaction amounts.
static List listOfAnything() { return (List) new ArrayList(); }
List stringList = listOfAnything();
List listOfInts() { return listOfAnything(); }
The generic type isn't monomorphised or even reified; within your method, you don't have access to the generic type T "passed" by the caller. If you need it, you have to pass it explicitly, as commonly used by deserialization libraries: static <T> T deserialize(String input, Class<T> cls) { ... }
Which can be used like this: MyStruct result = deserialize(input, MyStruct.class);
Note that the deserialization library has no way of knowing the type of the variable the caller is putting the result in, the caller had to pass it explicitly. This is because what the Java compiler actually generated from the code above was something like: static Object deserialize(String input, Class cls) { ... }
MyStruct result = (MyStruct) deserialize(input, MyStruct.class);Python's ordered arguments mixed with out of order labeled arguments is worse IMO: Tell me arguments in this order. Or not... I mean, you could just tell me which they are. Wait, I got two of these... F*ck this, I quit
Perl is well known for it's multitude of idiosyncrasies, this is one of the reasons why it can be frustrating to programmers who have better experience in more modern or rigidly structured languages.
(1) Un-named arguments come before any named arguments and should be passed in order (this is familiar to programmers in almost any mainstream language).
(2) Named arguments can then follow, in whatever order.
The interface is awkward however in that in other languages you would overload the function or pass a data structure, or reference of one, to manage additional optional parameters when you didn't want the extra arguments explicitly listed.
Python is a popular language and there are a ton of contributors who have developed third party packages. Unfortunately, these are not always well written or maintained and it can be easy to stumble across a bad one when looking for useful things. It's never fun to debug code in an unfamiliar language.
I adopted the contextual thinking fairly easily so it was not an issue for me but having worked with so many languages since perl I can see why this would be annoying to others. It's not the most unfortunate aspect though.
https://en.wikipedia.org/wiki/Principle_of_least_astonishmen...