Type check failure in generic return types with function parameters

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Type check failure in generic return types with function parameters

yless42
Hello all,

I was wondering if someone could explain why this code fails, since it seems trivial to me:


    case class TestClass(field: Double)
   
   
object Main {
     
def getField[T, U](obj: T, fn: T => U): U = fn(obj)
   
     
def main(args: Array[String]) = {
        val tt
= new TestClass(32.1)
        getField
(tt, _.field)
     
}
    }



The call to "getField" does not like its second parameter, when surely it should be able to determine that "[T] = TestClass".  My hunch is that it is for some reason getting a more generic type like "Any", but why?  Especially when this is fine, and surely only serves to confirm that "[T]" is what I think it is:


     getField(tt, {_.field}: TestClass => Double)



I've tried multiple variations, such as "{ x => x.field }" (doesn't work), explicitly giving "[TestClass, Double]" (works, but very verbose), "Function1[T, U]" instead of "T => U" (didn't help at all, and I didn't think it would).

This also works, and I'm not sure where the fundamental difference is in the type checking. The only problem is that this design doesn't work well for my end-target (this is obviously just a reduced example):




    case class TestClass(field: Double)
 
    object Main {
 
    implicit class Getter[T](obj: T) {
        def getField[U](fn: T => U): U = fn(obj)
   
  }
 
     
def main(args: Array[String]) = {
   
    val tt = new TestClass(32.1)
   
    tt.getField(_.field)
     
}
   
}




Any ideas?

Thanks,




Alex "Y_Less" Cole













--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Type check failure in generic return types with function parameters

Oliver Ruebenacker

     Hello,

  I don't know why it does not work, but I've seen similar cases before, and you can usually make them work by replacing

  def getField[T, U](obj: T, fn: T => U): U = fn(obj)

  with

  def getField[T, U](obj: T)(fn: T => U): U = fn(obj)

  The difference is that in the first case, the compiler tries to infer both T and U jointly, while in the second case, it first solves T separately before moving on to U.

     Best, Oliver


On Wed, Nov 23, 2016 at 6:33 AM, <[hidden email]> wrote:
Hello all,

I was wondering if someone could explain why this code fails, since it seems trivial to me:


    case class TestClass(field: Double)
   
   
object Main {
     
def getField[T, U](obj: T, fn: T => U): U = fn(obj)
   
     
def main(args: Array[String]) = {
        val tt
= new TestClass(32.1)
        getField
(tt, _.field)
     
}
    }



The call to "getField" does not like its second parameter, when surely it should be able to determine that "[T] = TestClass".  My hunch is that it is for some reason getting a more generic type like "Any", but why?  Especially when this is fine, and surely only serves to confirm that "[T]" is what I think it is:


     getField(tt, {_.field}: TestClass => Double)



I've tried multiple variations, such as "{ x => x.field }" (doesn't work), explicitly giving "[TestClass, Double]" (works, but very verbose), "Function1[T, U]" instead of "T => U" (didn't help at all, and I didn't think it would).

This also works, and I'm not sure where the fundamental difference is in the type checking. The only problem is that this design doesn't work well for my end-target (this is obviously just a reduced example):




    case class TestClass(field: Double)
 
    object Main {
 
    implicit class Getter[T](obj: T) {
        def getField[U](fn: T => U): U = fn(obj)
   
  }
 
     
def main(args: Array[String]) = {
   
    val tt = new TestClass(32.1)
   
    tt.getField(_.field)
     
}
   
}




Any ideas?

Thanks,




Alex "Y_Less" Cole













--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Senior Software Engineer, Diabetes Portal, Broad Institute

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Type check failure in generic return types with function parameters

Hugo Ferreira
For an explanation see:

http://stackoverflow.com/questions/13115053/scala-anonymous-function-missing-parameter-type-error

HTHs

On Wednesday, 23 November 2016 19:39:02 UTC, Oliver Ruebenacker wrote:

     Hello,

  I don't know why it does not work, but I've seen similar cases before, and you can usually make them work by replacing

  def getField[T, U](obj: T, fn: T => U): U = fn(obj)

  with

  def getField[T, U](obj: T)(fn: T => U): U = fn(obj)

  The difference is that in the first case, the compiler tries to infer both T and U jointly, while in the second case, it first solves T separately before moving on to U.

     Best, Oliver


On Wed, Nov 23, 2016 at 6:33 AM, <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="GL3LGL1KAQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">yle...@...> wrote:
Hello all,

I was wondering if someone could explain why this code fails, since it seems trivial to me:


    case class TestClass(field: Double)
   
   
object Main {
     
def getField[T, U](obj: T, fn: T => U): U = fn(obj)
   
     
def main(args: Array[String]) = {
        val tt
= new TestClass(32.1)
        getField
(tt, _.field)
     
}
    }



The call to "getField" does not like its second parameter, when surely it should be able to determine that "[T] = TestClass".  My hunch is that it is for some reason getting a more generic type like "Any", but why?  Especially when this is fine, and surely only serves to confirm that "[T]" is what I think it is:


     getField(tt, {_.field}: TestClass => Double)



I've tried multiple variations, such as "{ x => x.field }" (doesn't work), explicitly giving "[TestClass, Double]" (works, but very verbose), "Function1[T, U]" instead of "T => U" (didn't help at all, and I didn't think it would).

This also works, and I'm not sure where the fundamental difference is in the type checking. The only problem is that this design doesn't work well for my end-target (this is obviously just a reduced example):




    case class TestClass(field: Double)
 
    object Main {
 
    implicit class Getter[T](obj: T) {
        def getField[U](fn: T => U): U = fn(obj)
   
  }
 
     
def main(args: Array[String]) = {
   
    val tt = new TestClass(32.1)
   
    tt.getField(_.field)
     
}
   
}




Any ideas?

Thanks,




Alex "Y_Less" Cole













--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="javascript:" target="_blank" gdf-obfuscated-mailto="GL3LGL1KAQAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">scala-user+...@googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Senior Software Engineer, <a href="http://www.type2diabetesgenetics.org/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.type2diabetesgenetics.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFVE8xTGqlzANyuan2sVDo75uy-vA&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.type2diabetesgenetics.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFVE8xTGqlzANyuan2sVDo75uy-vA&#39;;return true;">Diabetes Portal, <a href="http://www.broadinstitute.org/" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.broadinstitute.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHloBLjHUP5Yhb6LyVOZ-JE5b4Y5A&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.broadinstitute.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHloBLjHUP5Yhb6LyVOZ-JE5b4Y5A&#39;;return true;">Broad Institute

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Type check failure in generic return types with function parameters

yless42
Thanks for that.  I had tried to check SO, but had no idea what to call it for the search.

On Thursday, 24 November 2016 14:34:03 UTC+1, Hugo Ferreira wrote:
For an explanation see:

<a href="http://stackoverflow.com/questions/13115053/scala-anonymous-function-missing-parameter-type-error" target="_blank" rel="nofollow" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fstackoverflow.com%2Fquestions%2F13115053%2Fscala-anonymous-function-missing-parameter-type-error\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHT6RxThlRKWXwZWjFGr3uWaGjawA&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fstackoverflow.com%2Fquestions%2F13115053%2Fscala-anonymous-function-missing-parameter-type-error\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHT6RxThlRKWXwZWjFGr3uWaGjawA&#39;;return true;">http://stackoverflow.com/questions/13115053/scala-anonymous-function-missing-parameter-type-error

HTHs

On Wednesday, 23 November 2016 19:39:02 UTC, Oliver Ruebenacker wrote:

     Hello,

  I don't know why it does not work, but I've seen similar cases before, and you can usually make them work by replacing

  def getField[T, U](obj: T, fn: T => U): U = fn(obj)

  with

  def getField[T, U](obj: T)(fn: T => U): U = fn(obj)

  The difference is that in the first case, the compiler tries to infer both T and U jointly, while in the second case, it first solves T separately before moving on to U.

     Best, Oliver


On Wed, Nov 23, 2016 at 6:33 AM, <[hidden email]> wrote:
Hello all,

I was wondering if someone could explain why this code fails, since it seems trivial to me:


    case class TestClass(field: Double)
   
   
object Main {
     
def getField[T, U](obj: T, fn: T => U): U = fn(obj)
   
     
def main(args: Array[String]) = {
        val tt
= new TestClass(32.1)
        getField
(tt, _.field)
     
}
    }



The call to "getField" does not like its second parameter, when surely it should be able to determine that "[T] = TestClass".  My hunch is that it is for some reason getting a more generic type like "Any", but why?  Especially when this is fine, and surely only serves to confirm that "[T]" is what I think it is:


     getField(tt, {_.field}: TestClass => Double)



I've tried multiple variations, such as "{ x => x.field }" (doesn't work), explicitly giving "[TestClass, Double]" (works, but very verbose), "Function1[T, U]" instead of "T => U" (didn't help at all, and I didn't think it would).

This also works, and I'm not sure where the fundamental difference is in the type checking. The only problem is that this design doesn't work well for my end-target (this is obviously just a reduced example):




    case class TestClass(field: Double)
 
    object Main {
 
    implicit class Getter[T](obj: T) {
        def getField[U](fn: T => U): U = fn(obj)
   
  }
 
     
def main(args: Array[String]) = {
   
    val tt = new TestClass(32.1)
   
    tt.getField(_.field)
     
}
   
}




Any ideas?

Thanks,




Alex "Y_Less" Cole













--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit <a href="https://groups.google.com/d/optout" rel="nofollow" target="_blank" onmousedown="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/d/optout&#39;;return true;">https://groups.google.com/d/optout.



--
Oliver Ruebenacker
Senior Software Engineer, <a href="http://www.type2diabetesgenetics.org/" rel="nofollow" target="_blank" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.type2diabetesgenetics.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFVE8xTGqlzANyuan2sVDo75uy-vA&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.type2diabetesgenetics.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNFVE8xTGqlzANyuan2sVDo75uy-vA&#39;;return true;">Diabetes Portal, <a href="http://www.broadinstitute.org/" rel="nofollow" target="_blank" onmousedown="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.broadinstitute.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHloBLjHUP5Yhb6LyVOZ-JE5b4Y5A&#39;;return true;" onclick="this.href=&#39;http://www.google.com/url?q\x3dhttp%3A%2F%2Fwww.broadinstitute.org%2F\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHloBLjHUP5Yhb6LyVOZ-JE5b4Y5A&#39;;return true;">Broad Institute

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.