I have java package There is Host class and Client class that can reveive many hosts
public final class Host {
public final String name;
public final int port;
}
public class SomeClient{...
public Client(Host... hosts) throws Exception {
....
}
I'm writing scala code to creating this client
// the hosts example is hosts1,host2,host3
def getClient(hosts:String) :SomeClient ={
val default_port:Int = 100
val hostsArr: Array[String] =hosts.split(",")
new Client (hostArr ???)
}
How can I map and convert scala array of strings to Host[], so the client will be created properly?
def getClient(hosts:String): SomeClient = {
val default_port:Int = 100
val hostsArr: Array[String] = hosts.split(",")
//Here you map over array of string and create Host object for each item
val hosts = hostsArr map { hostStr =>
val host = new Host()
//Rest of assignment
host
}
new Client(hosts:_*)
}
You can check the scala reference for repeated parameters section 4.6.2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With