scala - heterogenous mapping, dependent types at compile time -


trying use this trick, miles sabin, create function accepts parameter of set of predefined types.

val bool =  boolean val timestamp = new timestamp(date.gettime()) val str = "my string" 

and following should pass @ compile time

takevalue(bool) takevalue(timestamp) takevalue(str) 

but

where takevalue should fail takevalue(someintvalue) if implicit type int isn't defined.and failure @ compile time.

trait myconv[k] { type v; def convert: anyref => v }  def imakeconv[v0](con: anyref => v0) = new myconv[con.type] {   override type v = v0   val convert = con }    def takevalue(value:anyref)(implicit conv :myconv[value.type]) : \/[throwable,conv.v] = \/.fromtrycatch(conv.convert(value)) 

and

implicit val strany = imakeconv((x:any) => x.tostring) 

then want takevalue(str) work @ compile time takevalue(someintvalue) fail @ compile time since there isn't appropriate implicit defined it. want limit(at compile time) type of types takevalue can take , fail others.

certainly doing wrong here because when calling

takevalue("string") 

it throws following @ compile time

 not find implicit value parameter conv:   myconv[string("string")] - not enough arguments method takevalue: (implicit conv:   myconv[string("string")])scalaz.\/[throwable,conv.v].   unspecified value parameter conv. 

the meaning of .type misunderstood. type value.type type single value - value. if value known string, value.type not string, 1 string - value.

as result, code tries find myconv[value.type], of none exist, if there myconv[string] available.

have takevalue use type parameter instead:

def takevalue[t](value: t)(implicit conv: myconv[t]) : \/[throwable, conv.v] = \/.fromtrycatch(conv.convert(value)) 

or alternatively make type parameter of myconv contravariant:

trait myconv[-k] { type v; def convert: anyref => v } 

which allow myconv[t] used if value.type subtype of t.

you need different myconv , imakeconv:

trait myconv[k] { type v; def convert: k => v }  def imakeconv[k, v0](con: k => v0) = new myconv[k] {   override type v = v0   val convert = con } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -