Git Product home page Git Product logo

Comments (5)

cornerman avatar cornerman commented on May 31, 2024

How about using a typeclass for the implicit conversion of A => Modifier[A] to Modifier[A]? So you could have a typeclass AsModifier[F] with instances for [A] Modifier[A] and [A] A => Modifier[A]. Instead of accepting a Modifier and implicitly converting a function to Modifier, your method would then expect something that can be an AsModifier. This should not trigger a SAM conversion.

def func[T : AsModifier](modifier: T) = ???

from scala-dom-types.

raquo avatar raquo commented on May 31, 2024

Hm thanks, looks like a legit solution for this SAM problem in general. However, in this particular case I don't think it's a good fit because of probable performance impact on both compilation time (implicit search for every modifier, not just for thisNode => modifier) and runtime (I don't think Scala.js will be able to optimize away the typeclass layer, so probably extra allocated objects and extra function calls). This matters because in a frontend app method calls which accept Modifier/AsModifier params would be extremely plentiful.

I also asked this on SO btw.

from scala-dom-types.

raquo avatar raquo commented on May 31, 2024

I also tried

  trait MetaModifier[-El] extends AsModifier[El] {

    override def apply(el: El): Unit = {
      makeModifier(el)(el)
    }

    def makeModifier(el: El): Modifier[El]
  }

hoping the SAM conversion magic for a El => Modifier[El] literal will choose this trait instead of coercing Modifier[El] to Unit and magicking the whole literal into a Modifier[El], but no such luck, sigh.

from scala-dom-types.

cornerman avatar cornerman commented on May 31, 2024

However, in this particular case I don't think it's a good fit because of probable performance impact

Performance might be an issue, but maybe not even that bad (would need to try it out and see the generated code). I just find typeclasses more reliable and clear than implicit conversion functions :)

Interestingly, when requiring a trait mixin like (Modifier with AnyRef), the SAM conversion does not apply. Not sure, whether this is viable solution:

scala> trait F { def f(a:Int):String }
defined trait F
scala> def f(x:F with AnyRef) = x.f(1)
f: (x: F)String
scala> f((i:Int) => "a")
<console>:13: error: type mismatch;
 found   : Int => String
 required: F
       f((i:Int) => "a")

from scala-dom-types.

raquo avatar raquo commented on May 31, 2024

Thanks, that's a workable solution too.

I didn't want to compromise the simplicity of Modifier type signatures even a bit, and eventually I realized that I can just make the apply method non-abstract:

trait Modifier[-El] {
  def apply(el: El): Unit = ()
}

Admittedly, now you can potentially forget to provide an implementation for your Modifier subclass. But since end users by and large will not be creating modifiers on the fly, this seems like the best tradeoff to make, and a reasonable price to pay to completely disable undesired SAM sugar.

from scala-dom-types.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.