tuple-packing

scala coalesces multiple function call parameters into a Tuple — can this be disabled?

不羁岁月 提交于 2019-12-28 16:00:16
问题 This is a troublesome violation of type safety in my project, so I'm looking for a way to disable it. It seems that if a function takes an AnyRef (or a java.lang.Object), you can call the function with any combination of parameters, and Scala will coalesce the parameters into a Tuple object and invoke the function. In my case the function isn't expecting a Tuple, and fails at runtime. I would expect this situation to be caught at compile time. object WhyTuple { def main(args: Array[String]):

scala coalesces multiple function call parameters into a Tuple — can this be disabled?

人盡茶涼 提交于 2019-11-28 10:17:14
This is a troublesome violation of type safety in my project, so I'm looking for a way to disable it. It seems that if a function takes an AnyRef (or a java.lang.Object), you can call the function with any combination of parameters, and Scala will coalesce the parameters into a Tuple object and invoke the function. In my case the function isn't expecting a Tuple, and fails at runtime. I would expect this situation to be caught at compile time. object WhyTuple { def main(args: Array[String]): Unit = { fooIt("foo", "bar") } def fooIt(o: AnyRef) { println(o.toString) } } Output: (foo,bar) No

Tuple unpacking order changes values assigned

不想你离开。 提交于 2019-11-27 20:59:16
I think the two are identical. nums = [1, 2, 0] nums[nums[0]], nums[0] = nums[0], nums[nums[0]] print nums # [2, 1, 0] nums = [1, 2, 0] nums[0], nums[nums[0]] = nums[nums[0]], nums[0] print nums # [2, 2, 1] But the results are different. Why are the results different? (why is the second one that result?) Prerequisites - 2 important Points Lists are mutable The main part in lists is that lists are mutable. It means that the values of lists can be changed. This is one of the reason why you are facing the trouble. Refer the docs for more info Order of Evaluation The other part is that while

Tuple unpacking order changes values assigned

99封情书 提交于 2019-11-27 04:29:29
问题 I think the two are identical. nums = [1, 2, 0] nums[nums[0]], nums[0] = nums[0], nums[nums[0]] print nums # [2, 1, 0] nums = [1, 2, 0] nums[0], nums[nums[0]] = nums[nums[0]], nums[0] print nums # [2, 2, 1] But the results are different. Why are the results different? (why is the second one that result?) 回答1: Prerequisites - 2 important Points Lists are mutable The main part in lists is that lists are mutable. It means that the values of lists can be changed. This is one of the reason why you