A question regarding type parameter inference

View: New views
3 Messages — Rating Filter:   Alert me  

A question regarding type parameter inference

by TomerL :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi all,

I have tried to use implicit values in a parameterized function and I found out that the type parameter of the function is not being inferred in the curried function (implicit values must be given curried). I wanted to ask if it is possible to force the type inference to infer only the trait type.

An example is the following:
---------------------------------------------------
class A
trait T
trait Printer[S <: T] {def print():Unit}

def pr[S1 <: A, S2 <: T](a: S1 with S2)(implicit p: Printer[S2]) = p.print()
---------------------------------------------------

I would like to be able to have the type of the argument decomposed into a class and a trait, so I can define the implicit value over the type of the trait only. Like in the following call:

---------------------------------------------------
pr(new A with T)(new Printer[T] {def print() = Console.println("1")})
---------------------------------------------------

Here I get the error "required: Printer[A with T{ ... }]" but "found   : java.lang.Object with Printer[T]{ ... }"
Which seems to imply that the parameters of 'pr' get the most specific typing and is not decomposed. but it seems that such a decomposition of type is possible because if I dont use a curried version:

---------------------------------------------------
def pr2[S1 <: A, S2 <: T](a: S1 with S2,p: Printer[S2]) = p.print() // not curried
pr2(new A with T,new Printer[T] {def print() = Console.println("1")}) // no problem.
---------------------------------------------------

the call succeeds .

It seems to me that in the curried version we are working in two phases and first define the type parameter of the partially-applied function to be 'A with T{ ... }' although I specify its type to be <: T.

Of course, if I supply the trait type as a parameter to the curried function it also infers the type correctly.

I apologize in advance if my formulation of the problem is not very clear. I try to ask several questions about the type inference of a trait as type parameter, about the type inference of type parameters in curried functions and about the possibility to decompose types using sentences like [T,S] T with S.
The main idea is that I want to use implicit parameters which are identified according to traits and not classes and in the curried functions it seems to me the type is inferred in a too specific way (A with T) and not as T. I think it is also more intuitive to have the type inference behave the same in the curried and un-curried versions.

Thanks,
Tomer

Re: [scala] A question regarding type parameter inference

by Adriaan Moors-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

By design, type inference only considers the first list of arguments (similarly for overloading, which is decided purely on the type of the arguments of the first argument list). 

hth
adriaan

On Thu, Nov 5, 2009 at 5:17 PM, TomerL <shaolintl@...> wrote:

Hi all,

I have tried to use implicit values in a parameterized function and I found
out that the type parameter of the function is not being inferred in the
curried function (implicit values must be given curried). I wanted to ask if
it is possible to force the type inference to infer only the trait type.

An example is the following:
---------------------------------------------------
class A
trait T
trait Printer[S <: T] {def print():Unit}

def pr[S1 <: A, S2 <: T](a: S1 with S2)(implicit p: Printer[S2]) = p.print()
---------------------------------------------------

I would like to be able to have the type of the argument decomposed into a
class and a trait, so I can define the implicit value over the type of the
trait only. Like in the following call:

---------------------------------------------------
pr(new A with T)(new Printer[T] {def print() = Console.println("1")})
---------------------------------------------------

Here I get the error "required: Printer[A with T{ ... }]" but "found   :
java.lang.Object with Printer[T]{ ... }"
Which seems to imply that the parameters of 'pr' get the most specific
typing and is not decomposed. but it seems that such a decomposition of type
is possible because if I dont use a curried version:

---------------------------------------------------
def pr2[S1 <: A, S2 <: T](a: S1 with S2,p: Printer[S2]) = p.print() // not
curried
pr2(new A with T,new Printer[T] {def print() = Console.println("1")}) // no
problem.
---------------------------------------------------

the call succeeds .

It seems to me that in the curried version we are working in two phases and
first define the type parameter of the partially-applied function to be 'A
with T{ ... }' although I specify its type to be <: T.

Of course, if I supply the trait type as a parameter to the curried function
it also infers the type correctly.

I apologize in advance if my formulation of the problem is not very clear. I
try to ask several questions about the type inference of a trait as type
parameter, about the type inference of type parameters in curried functions
and about the possibility to decompose types using sentences like [T,S] T
with S.
The main idea is that I want to use implicit parameters which are identified
according to traits and not classes and in the curried functions it seems to
me the type is inferred in a too specific way (A with T) and not as T. I
think it is also more intuitive to have the type inference behave the same
in the curried and un-curried versions.

Thanks,
Tomer
--
View this message in context: http://old.nabble.com/A-question-regarding-type-parameter-inference-tp26215790p26215790.html
Sent from the Scala mailing list archive at Nabble.com.


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm


Re: [scala] A question regarding type parameter inference

by TomerL :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks, this helped. I understand now this problem can also be solved by using type parameters in the argument given (and not to "Decompose" the type).

Thanks again,
Tomer

2009/11/5 Adriaan Moors <adriaan.moors@...>
By design, type inference only considers the first list of arguments (similarly for overloading, which is decided purely on the type of the arguments of the first argument list). 

hth
adriaan

On Thu, Nov 5, 2009 at 5:17 PM, TomerL <shaolintl@...> wrote:

Hi all,

I have tried to use implicit values in a parameterized function and I found
out that the type parameter of the function is not being inferred in the
curried function (implicit values must be given curried). I wanted to ask if
it is possible to force the type inference to infer only the trait type.

An example is the following:
---------------------------------------------------
class A
trait T
trait Printer[S <: T] {def print():Unit}

def pr[S1 <: A, S2 <: T](a: S1 with S2)(implicit p: Printer[S2]) = p.print()
---------------------------------------------------

I would like to be able to have the type of the argument decomposed into a
class and a trait, so I can define the implicit value over the type of the
trait only. Like in the following call:

---------------------------------------------------
pr(new A with T)(new Printer[T] {def print() = Console.println("1")})
---------------------------------------------------

Here I get the error "required: Printer[A with T{ ... }]" but "found   :
java.lang.Object with Printer[T]{ ... }"
Which seems to imply that the parameters of 'pr' get the most specific
typing and is not decomposed. but it seems that such a decomposition of type
is possible because if I dont use a curried version:

---------------------------------------------------
def pr2[S1 <: A, S2 <: T](a: S1 with S2,p: Printer[S2]) = p.print() // not
curried
pr2(new A with T,new Printer[T] {def print() = Console.println("1")}) // no
problem.
---------------------------------------------------

the call succeeds .

It seems to me that in the curried version we are working in two phases and
first define the type parameter of the partially-applied function to be 'A
with T{ ... }' although I specify its type to be <: T.

Of course, if I supply the trait type as a parameter to the curried function
it also infers the type correctly.

I apologize in advance if my formulation of the problem is not very clear. I
try to ask several questions about the type inference of a trait as type
parameter, about the type inference of type parameters in curried functions
and about the possibility to decompose types using sentences like [T,S] T
with S.
The main idea is that I want to use implicit parameters which are identified
according to traits and not classes and in the curried functions it seems to
me the type is inferred in a too specific way (A with T) and not as T. I
think it is also more intuitive to have the type inference behave the same
in the curried and un-curried versions.

Thanks,
Tomer
--
View this message in context: http://old.nabble.com/A-question-regarding-type-parameter-inference-tp26215790p26215790.html
Sent from the Scala mailing list archive at Nabble.com.


Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm