Sunday, 15 June 2014

scala - surrounding function for adding context -



scala - surrounding function for adding context -

i trying write function can add together context functions given in parameters.

the thought here

object illustration { def withone(f : => t) = { val = 1 //some context f } def foo() = withone { println(a) } }

i think context passed in implicit. thought not have content of f constraint surrounding function f should able utilize context or not.

for way seen that

object illustration { def withone(f : => int => t) = { val = 1 //some context f(a) } def foo() = withone { => println(a) } }

but forces declare 'a' witch not obvious others devs :x

i'm afraid cannot work around this, since cannot inject implicit function.

there's proposal add together feature in typelevel/scala fork, seems hard accomplish of today.

my suggestion here utilize proper naming, won't surprise users. instance if provide method like:

def withconnection[a](f: connection => a): = { seek { val conn = ??? f(conn) } { conn.close() } }

it won't surprise me do:

withconnection { implicit c => // db stuff }

scala functional-programming

No comments:

Post a Comment