1

My problem is quite simple : I want to have an implicit conversion of value to function if they are not already function. I plan to use a type safe pattern by requesting the instantiation of an implicit parameter (if the valueis a function the implicit creation fails). However I do not see how to test that a value is not a function

I learned type safe pattern from user Beryllium in one of my previous question. See : Type safe method chaining that doesn't allow repeats of operations

The implicit I've implemented is working, but too well. I want to transform not function expression to specific-application-default function automatically

 implicit def defaultExecutionUnitParameterNReturn(a: Any): Unit => MyDefaultReturn = 
{u : Unit => a }.andThen(_ => defaultReturn())

However my application would fail if the user implements "a" as a function

So my first idea was something like

 implicit def defaultExecutionUnitParameterNReturn[A](a: A)(implicit e : A =!= Function) Unit => MyDefaultReturn = 
{u : Unit => a }.andThen(_ => defaultReturn())

where implicit =!=[A,B] fails if A and B are the same type. But "Function" does not exists

Community
  • 1
  • 1
Coo LHibou
  • 149
  • 5
  • Coo, please paste your code. How do you try to convert object into function and how it fails? – pawel.panasewicz Oct 25 '13 at 10:50
  • and i'm interested in type safe pattern or do you mean type class? – 4lex1v Oct 25 '13 at 13:24
  • Please don't use the word "object" as a synonym for "value," since in Scala `object` is a keyword for creating a special kind of value. Secondly, don't use "function" as a synonym for "method," since their quite distinct in Scala and only `FunctionN` are first-class values. When you ignore these distinctions, your question becomes ambiguous and hard to interpret. – Randall Schulz Oct 25 '13 at 15:20
  • @Randall Schulz : you absolutely right, i corrected to "object" into "value". "Function" was correctly used. – Coo LHibou Oct 28 '13 at 09:07
  • @AlexIv post edited with a link to the type safe pattern – Coo LHibou Oct 28 '13 at 09:09
  • @pawel.panasewicz post edited with a code example – Coo LHibou Oct 28 '13 at 09:09

2 Answers2

2

You need to place your implicit conversions in 2 traits

trait Implicits extends ImplicitsLow {
  implicit def convertFunction[T, R](f: T => R) = ???
}

trait ImplicitsLow {
  implicit def convert[T](t: T) = ???
}

Then you can observe that the function conversion is used in preference to the value one:

val result1: String = (i: Int) => i + 1
val result2: String = 1

// prints (function, value)   
println((result1, result2))
Eric
  • 15,494
  • 38
  • 61
0

Investigate below code snipped. This is standard implicit convertion example. toFunnction0 takes anything and converts it into Function0[R] or just simplified () => R.

implicit def toFunnction0[R](r: => R): Function0[R] = () => r

def iWantFunction0(f0: () => String) = {
  f0()
}

def testFun = {println("computing string in testFun..."); "ABC"} //every time when called block of code will run
val abcVal = "ABC" //this is computed only once

iWantFunction0(testFun)

//here abcVal is not a function, so implicit works. 
//toFunnction0 is in scope, so compiler will translate it into
//iWantFunction0(toFunnction0(abcVal))  
iWantFunction0(abcVal)  
pawel.panasewicz
  • 1,831
  • 16
  • 27