Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I compose a function that has an implicit argument?

Tags:

scala

I'm playing around with higher kinds, and I'm trying to use compose. I've got the following code:

def p2( a : Int) = a + 2
def p3( a : Int) = a + 3
val p5 = p2 _ compose p3
def pn3[T](n : T)(implicit ev : Numeric[T]) = ev.plus(n, ev.fromInt(3))
val pn5 = p2 _ compose pn3

It all works until the last line:

error: could not find implicit value for parameter ev: Numeric[T]

Which makes sense but how do I tell it, "I want Numeric[Int]!"

like image 538
Michael Lorton Avatar asked Sep 22 '11 11:09

Michael Lorton


People also ask

What are implicit arguments?

Implicit arguments are arguments that occur in Logical Form, but are omitted in the syntax. Consider the following sentences: (1) Mary was run over by a car. (2) Mary was run over with a car. (21) implies that there is no perceivable or known agent.

What is implicit parameter in C++?

The this variable is a product of C++'s member calling syntax. Instead of the programmer having to explicitly declare a parameter identifying the object (as in Ada), C++ does this automatically for you (this). In most implementations, C++ parameters are bound to offsets to locations on the stack or to registers.


1 Answers

Trial and error ;)

val pn5 = p2 _ compose pn3[Int]
like image 124
agilesteel Avatar answered Sep 28 '22 03:09

agilesteel