- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Why is Nothing being inferred
Fri, 2011-06-24, 03:20
scala> object JsTypes { class JsAny protected ; final class JsBoolean private extends JsAny final class JsNumber private extends JsAny final class JsString private extends JsAny final class JsDate private extends JsAny final class JsRegex private extends JsAny final class JsObj private extends JsAny final class JsArray private extends JsAny final class JsFunction1[-P <: JsAny, +R <: JsAny] private extends JsAny final class JsVoid private extends JsAny}import JsTypes._defined module JsTypesimport JsTypes._
scala> trait JsExp[+T <: JsAny] { def render: String}defined trait JsExp
scala> trait JsRaw[T <: JsAny] extends JsExp[T]object JsRaw { def apply[T <: JsAny](rendering: => String) = new JsRaw[T] { def render = rendering }}defined trait JsRawdefined module JsRaw
scala> case class Applicable[-P<:JsAny,+R<:JsAny](f: JsExp[JsFunction1[P,R]]) extends (JsExp[P]=>JsExp[R]) { def apply(p: JsExp[P]): JsExp[R] = JsRaw[R](f.render+"("+p.render+")") }defined class Applicable
scala> implicit def func1toApplicable[P<:JsAny,R<:JsAny,T <: JsExp[JsFunction1[P,R]]](f: T): Applicable[P,R] = Applicable[P,R](f) func1toApplicable: [P <: JsTypes.JsAny,R <: JsTypes.JsAny,T <: JsExp[JsTypes.JsFunction1[P,R]]](f: T)Applicable[P,R]
scala> case class JsIdent[T <: JsAny](ident: Symbol) extends JsExp[T] { def render = ident.name}defined class JsIdent
scala> val alert: JsExp[JsFunction1[JsString,JsVoid]] = JsIdent('alert) alert: JsExp[JsTypes.JsFunction1[JsTypes.JsString,JsTypes.JsVoid]] = JsIdent('alert)
scala> func1toApplicable(alert)<console>:19: error: inferred type arguments [Nothing,Nothing,JsExp[JsTypes.JsFunction1[JsTypes.JsString,JsTypes.JsVoid]]] do not conform to method func1toApplicable's type parameter bounds [P <: JsTypes.JsAny,R <: JsTypes.JsAny,T <: JsExp[JsTypes.JsFunction1[P,R]]] func1toApplicable(alert) ^
Why is it inferring Nothing?
Ideally func1toApplicable would have T <% JsExp[JsFunction1[P,R], and then you could just write s: JsExp[JsString] => alert(s)
Fri, 2011-06-24, 09:07
#2
Re: Re: Why is Nothing being inferred
You must have P and R appear in the argument list somehow. T <:
JsExp[JsFunction1[P,R]] will only verify that T conforms to a type
expression on P and R, but won't be used to infer P and R.
On Thu, Jun 23, 2011 at 9:35 PM, Naftoli Gugenheim wrote:
>>
>> Ideally func1toApplicable would have T <% JsExp[JsFunction1[P,R], and then
>> you could just write
>> s: JsExp[JsString] => alert(s)
>
> Sorry for not explaining better. The reason I wanted it to be a view bound
> not a type bound is because I wanted to have this:
> object JsDef {
> implicit def def2ref[T <: JsAny](d: JsDef[T, _]): JsExp[T] =
> JsIdent(d.name)
> }
> class JsDef[T <: JsAny, S : ToJs.To[T]#From](init: S) {
> def name: Symbol =
> Symbol(getClass.getName.toList.reverse.dropWhile('$'==).takeWhile('$'!=).reverse.mkString)
> def render = "var "+name.name+"="+init.render+";"
> }
> without making JsDef extend JsExp[T], and use a JsDef instead of an actual
> JsExp.
> I'd still appreciate if there's a solution, but meanwhile I just made JsDef
> extend JsExp, and took out funct1ToApplicable's T type parameter and just
> made JsExp[JsFunction1[P,R]] be f's type directly.
> Thanks.
>
Fri, 2011-06-24, 19:07
#3
Re: Re: Why is Nothing being inferred
I hear that that's the problem, but there aren't any P or R values to pass --- they're going to be passed (to and from) Applicable#apply.So there's no solution?
On Fri, Jun 24, 2011 at 3:54 AM, Lex <lexn82@gmail.com> wrote:
On Fri, Jun 24, 2011 at 3:54 AM, Lex <lexn82@gmail.com> wrote:
You must have P and R appear in the argument list somehow. T <:
JsExp[JsFunction1[P,R]] will only verify that T conforms to a type
expression on P and R, but won't be used to infer P and R.
On Thu, Jun 23, 2011 at 9:35 PM, Naftoli Gugenheim <naftoligug@gmail.com> wrote:
>>
>> Ideally func1toApplicable would have T <% JsExp[JsFunction1[P,R], and then
>> you could just write
>> s: JsExp[JsString] => alert(s)
>
> Sorry for not explaining better. The reason I wanted it to be a view bound
> not a type bound is because I wanted to have this:
> object JsDef {
> implicit def def2ref[T <: JsAny](d: JsDef[T, _]): JsExp[T] =
> JsIdent(d.name)
> }
> class JsDef[T <: JsAny, S : ToJs.To[T]#From](init: S) {
> def name: Symbol =
> Symbol(getClass.getName.toList.reverse.dropWhile('$'==).takeWhile('$'!=).reverse.mkString)
> def render = "var "+name.name+"="+init.render+";"
> }
> without making JsDef extend JsExp[T], and use a JsDef instead of an actual
> JsExp.
> I'd still appreciate if there's a solution, but meanwhile I just made JsDef
> extend JsExp, and took out funct1ToApplicable's T type parameter and just
> made JsExp[JsFunction1[P,R]] be f's type directly.
> Thanks.
>
Sorry for not explaining better. The reason I wanted it to be a view bound not a type bound is because I wanted to have this:
object JsDef { implicit def def2ref[T <: JsAny](d: JsDef[T, _]): JsExp[T] = JsIdent(d.name) }class JsDef[T <: JsAny, S : ToJs.To[T]#From](init: S) { def name: Symbol = Symbol(getClass.getName.toList.reverse.dropWhile('$'==).takeWhile('$'!=).reverse.mkString) def render = "var "+name.name+"="+init.render+";"}
without making JsDef extend JsExp[T], and use a JsDef instead of an actual JsExp.
I'd still appreciate if there's a solution, but meanwhile I just made JsDef extend JsExp, and took out funct1ToApplicable's T type parameter and just made JsExp[JsFunction1[P,R]] be f's type directly.
Thanks.