0

I'd like to declare a function that receives a string and a Tuple2 with first value as boolean and second value as any type. The tuple2 should have a default value in case its not delivered to the function

I tried the following code to set the boolean as false but I failed miserably.

def setSet(key: String, value: Any, tuple2: Tuple2[Boolean, Any] = tuple2._1 = false) 
Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
PloniStacker
  • 574
  • 4
  • 23
  • 1
    If possible try to avoid `Any`, but since `tuple2` takes `Any` value, you can set anything as value, so perhaps try `None` like so `def setSet(key: String, value: Any, tuple2: (Boolean, Any) = (false, None))` – Mario Galic Sep 18 '19 at 12:49
  • 1
    What should be the value of `tuple2` if `setSet("foo", "bar")` is called? You can't create a two element tuple with only one value. Or were you expecting the `value` argument to be used for that? – jq170727 Sep 18 '19 at 12:51
  • 1
    Can you explain what do you want to do? `Any` is almost always a **code smell**. Also, you proposed solution does not make too much sense. – Luis Miguel Mejía Suárez Sep 18 '19 at 13:16
  • You guys were correct as my solution didn't work eventually... What I need is to create a default value Tuple2(Boolean,) the default has to be false for the first Tuple value, the second type is irrelevant as long as it is false ( (I'll provide the type if I override the default values) – PloniStacker Sep 18 '19 at 13:54
  • 1
    Well you can just `setSet(key: String, value: Any, tuple: (Boolean, Any) = (false, 0)` or really just whatever, instead of the zero, even you old solution would worked. But the thing is, that does not make too much sense. As I said, are you sure you really need an `Any` in the first place? That is usually sign of a bad design. – Luis Miguel Mejía Suárez Sep 18 '19 at 14:55
  • I am not sure Any is the correct solution but I do know that I need to have the ability to place either String or Int at different occasions. In c# for example I would use generics instead. – PloniStacker Sep 18 '19 at 17:29

1 Answers1

0

Try to overload method

def setSet(key: String, value: Any, tuple2: Tuple2[Boolean, Any]): Unit = println(s"key=$key, value=$value, tuple2=$tuple2")

def setSet(key: String, value: Any, any: Any): Unit = setSet(key, value, (false, any))

setSet("a", "b", 1) // key=a, value=b, tuple2=(false,1)
setSet("a", "b", (true, 2)) // key=a, value=b, tuple2=(true,2)
Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66