Comments (5)
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.
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.
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.
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.
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)
- Make it possible to have a different API for cls / className / classNames HOT 4
- Unescaped text entity -- raw() HOT 9
- Separation of reflected attributes and attributes with complex keys HOT 5
- Automatically generate dom types HOT 4
- Key renaming proposals HOT 4
- Discussion about lazy val definitions HOT 8
- implicit def eventPropToEventPropTransformation interferes sometimes HOT 2
- img loading attribute not supported HOT 2
- Support fullscreenchange event HOT 2
- Merge colliding html and svg attr names into shared namespace. HOT 3
- More complete code example HOT 1
- Aria Attributes should be usable from SVG too, not just HTML HOT 3
- Add flow-relative CSS properties HOT 2
- Server Side Rendering HOT 4
- Support HTML Dialog Element HOT 6
- Switch definitions from lazy vals to vals? HOT 2
- RFC: Use concrete types instead of generic type params? HOT 20
- Add info about supported tags to props/attrs HOT 6
- Publish defs (and generators?) for popular web component libraries, e.g. UI5, Material HOT 10
- Add ff4s to list of UI libs that use scala-dom-types HOT 13
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from scala-dom-types.