logo       

Implicit parameters: msg#00004

lang.scala

Subject: Implicit parameters

Consider:

def log(msg: String)(implicit title: String) =
Console.println("Log: " + title + '|' + msg);
def log(msg: String): unit = log(msg)("Default");

implicit val title = "Title";
log("Some message");
log("Another message")("Direct Title");

Scala doesn't currently have default values for parameters, but we can
create multiple versions of a function that call a basic version, as
above. The compiler I have now chooses the implicit version of the
function always.

I wonder about the following syntax for Param:

implicit varname: Type [=#symbolFilter][=constvalue]

def log(msg: String)(implicit title: String =#logTitle ="No Title")

I can then write:

def func = {
def inner = {
implicit val logTitle = "inner";
log("A message");
}
log("Hello");
}

I don't think the symbol lookup has any effect on type safety. The
implicit parameter as currently specified is already doing a search of
the context for a symbol of the right type; =# directs the search to
match only symbols tagged with implicit that match the filter. If the
defaultValue is present that value is used if the implicit search fails.

I like the unification of default parameters and views that the current
implicit approach represents, but wonder if the symbol selection filter
provides additional and necessary precision, particularly when type does
not provide enough of a discriminator.



RJ



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise