Java mappa non è di tipo sicuro, in particolare il metodo get
ha questa firma:
public V get(Object key);
in modo da poter usare qualcosa come una chiave. In Scala si sta vedendo cosiddetta auto-tupling, qualcosa che è deprecato in Scala 2.11, quindi se si compila il progetto con -deprecation
, si vedrà:
[warn] ... Adaptation of argument list by inserting() has been deprecated: leaky (Object-receiving) target makes this especially dangerous.
[warn] signature: HashMap.get(x$1: Any): V
[warn] given arguments: <none>
[warn] after adaptation: HashMap.get((): Unit)
[warn] "Test " + map.get("key1") + " " + map.get() + " " + map.get("key1", "key2") + " " + map.get("key1", "key2", "key3")
[warn] ^
È possibile trasformarla in un errore con il -Xfuture
flag di compilazione:
[error] ... Adaptation of argument list by inserting() has been removed.
[error] signature: HashMap.get(x$1: Any): V
[error] given arguments: <none>
[error] "Test " + map.get("key1") + " " + map.get() + " " + map.get("key1", "key2") + " " + map.get("key1", "key2", "key3")
[error] ^
Auto-tupling significa map.get()
saranno trattati come map.get(())
e map.get("key1", "key2")
sarà trattata come map.get(("key1", "key2"))
.
Si consiglia di utilizzare i tipi di raccolta di Scala, a meno che non si abbia una ragione molto particolare per non farlo.
fonte
2014-12-02 22:05:16
'-Xfuture' sembra avvisare solo per i paren vuoti,' -Ywarn-adapt-args' avverte sugli altri casi di auto-tupling. – Dimitri