r/coding • u/waqar_kalim • Mar 19 '22
JavaScript — Using The Spread Operator (…)
https://medium.com/p/405266faf42b0
Mar 19 '22
[deleted]
6
u/not-just-yeti Mar 19 '22 edited Mar 23 '22
Lisp (1958) had(has) varargs, and unquote-splice:
(define (f arg1 arg2 . others) `(,arg2 99 ,arg1 "and" ,@others "etc.")) ; Note: the preceding-comma means "unquote", not delimiting args. (f 2 3 4 5) ; returns '(3 99 2 "and" 4 5 "etc.")Java also has varargs:
void main(String... args) { System.out.println(args[2]); }.But what Java doesn't quite do properly (that javascript, python, lisp do) is calling the varargs function when your desired-values are already in a list/array. (Python, add
*before your list when calling; javascript add...before your list when calling; lisp uses the functionapplyinstead of adding more syntax.)In Java, if you pass an array to a function expecting many items, it will also "spread" the array for you, but that makes it a bit ambiguous: If the function expects
Object..., and you pass it an array, did you mean to have the values spread out, or did you mean to pass a single array-object to the function? (Java interprets it as the former, so you'd have to realize that was happening, and wrap your array into another array-of-size-1.)
0
Mar 19 '22
[deleted]
2
u/countlictor Mar 19 '22
There's syntactic sugar for defining a type as
type || null, and combined with the tsconfig setting enforcing explicit null checks will get you what you're after.class Sample { public optionalProperty?: String; }0
Mar 19 '22
[deleted]
1
u/Jestar342 Mar 19 '22
Yes but types are not guarantees. You can define data types being received from an API, but if the API neglects to include a property, and you try to destructure it, your app will crash.
How on earth do you figure that that is an argument against typescript?
-31
u/[deleted] Mar 19 '22
... not making r/gay_irl joke, not making r/gay_irl joke, not making r/gay_irl joke ...