Skip to content

Instantly share code, notes, and snippets.

@sarveshseri
Last active September 20, 2019 17:55
Show Gist options
  • Save sarveshseri/425d9b0556cb0729666d276a9b012efb to your computer and use it in GitHub Desktop.
Save sarveshseri/425d9b0556cb0729666d276a9b012efb to your computer and use it in GitHub Desktop.
Mysteries of Scala
def iWantATuple(tuple: (String, String)): println(tuple)
// what auto tupling does ?
// it tries to fit the aruments as tuples if possible
// the following will work even when its not a tuple
iWantATuple("a", "b")
// this is infamous for absurd compiler messages whenever a Java method is invloved and you pass wrong
// number of arguments scala will try the last ditch effort of fitting your args as tuple, will fail and
// then tell you that you are passing the wrong tuple... and you will wonder... where scalac... where?
// this can be turned off by the compiler flag "-Yno-adapted-args"
// Or you can be warnings of such sorcery with the compiler flag "-Ywarn-adapted-args"
public class UmbrellaCorpsClient {
// since we are Umbrella Corps...
// we write umbrella methods...
// which can deal with types of names...
// but just this is not umbrella enough...
// what if there are multiple name and values...
// you might ask... do we even need non-String names... but we are hardcore umbrella
// We are hating this counter though... so specific with type... this guys is one of those integer-supremacist...
public void doWith(Integer counter, Object name, Object value, Object... nameValues) {
System.out.println(counter.toString() + "::" + name.toString() + ":" + value.toString());
}
// and since we hated that intger guy... lets remove him...
public void doWith(Object key, Object value, Object... nameValues) {
doWith()
System.out.println(objects);
}
}
object WeNeedUmbrellaCorpsClient {
val umbrellaCorpsClient: UmbrellaCorpsClient = ???
val integer = new java.lang.Integer(10)
// this will not work
doWith(integer, "one", "one's value is just one. It's called one, does it not ?")
// but our innocent call above will not compile...
// because an integer is also an object...
// so the above will match with both
// public void doWith(Integer counter, Object name, Object value)
// public void doWith(Object name, Object value, Object... valueNames)
// and scala will just not let you proceeed...
// to fix this particular problem... we will need to add empty varargs to the call
doWith(integer, "one", "one's value is just one. It's called one, does it not ?", Nil: _*)
// this case can be worked-around... but there are many others which could not be avoided...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment