Comments (6)
You're right, the template TExtByImplicit.scala
doesn't work correctly in your case. Armed with Scala 2.10, please try instead something like
final class ExtGraphNode[N, E[X] <: EdgeLikeIn[X]](g: Graph[N,E])(node: g.NodeT) {
def inHead = node.inNeighbors.head
}
val g = Graph(1 ~> 2, 2 ~> 3)
implicit def toExt(node: g.NodeT) = new ExtGraphNode(g)(node)
(g get 1).inHead.shortestPathTo(g get 3)
Looking forward to your feedback
Peter
from scala-graph.
Thanks for the fast response. I tried your code and it works as it is but If we rename the graph parameter the way shown below, the code will not compile asking for the value g2:
final class ExtGraphNode[N, E[X] <: EdgeLikeIn[X]](g2: Graph[N,E])(node: g2.NodeT) {
def inHead = node.inNeighbors.head
}
val g = Graph(1 ~> 2, 2 ~> 3)
implicit def toExt(node: g.NodeT) = new ExtGraphNode(g)(node)
(g get 1).inHead.shortestPathTo(g get 3)
That is, g in g.NodeT is coming from the instance of the graph object rather from the first parameter list which makes it impossible to implement enrich my library pattern. Perhaps it has something to do with the general idea of enriching the inner class.
from scala-graph.
After giving some thought to the issue I suspect that for the original code to work the compiler needs to be able to infer that the containingGraph instance of the node_ parameter is the same as the instance g which is impossible at the compile time. A simple but not very elegant workaround is to cast the returned node.
(g get 1).inHead.asInstanceOf[g.NodeT].shortestPathTo(g get 3)
Let me know if you have any other ideas.
from scala-graph.
Gleb,
Thanks for investigating.Casting does help but I'm not satisfied with it,
either. Clearly, we must find out the 'right way'. I'll look into it in the
following days. You may use asInstanceOf as a workaround so long.
2013/6/6 Gleb Sizov [email protected]
After giving some thought to the issue I suspect that for the original
code to work the compiler needs to be able to infer that the
containingGraph instance of the node_ parameter is the same as the instance
g which is impossible at the compile time. A simple but not very elegant
workaround is to cast the returned node.(g get 1).inHead.asInstanceOf[g.NodeT].shortestPathTo(g get 3)
Let me know if you have any other ideas.
—
Reply to this email directly or view it on GitHubhttps://github.com//issues/4#issuecomment-19028924
.
from scala-graph.
Hi Gleb,
you may find two further node enrichment alternatives at [https://github.com/scala-graph/scala-graph/blob/master/core/src/test/scala/custom/TExtByImplicit.scala]. I'd strongly prefer the way outlined in 'test_nodeEnrichment_3' because it is fully type safe and requires less resources at run time than 'test_nodeEnrichment_2'. The only drawback is that you need to instantiate ExtGraph and import the implicit conversion for each graph instance.
Can you go with it?
Peter
from scala-graph.
Hi Peter,
I will go for 'test_nodeEnrichment_3' because of the reasons you outlined above. In general, type safe enrichment of inner classes in Scala seems to require the kind of boilerplate code you have in 'test_nodeEnrichment_3'. I posted a relevant question on stackoverflow: http://stackoverflow.com/questions/16977730/enrich-an-inner-class
Thanks for help,
Gleb
from scala-graph.
Related Issues (20)
- Adding code for graph layout HOT 6
- Scala.js 1.1.0 support HOT 1
- Problem when trying to convert JSON to a Graph HOT 7
- Weird behaviour for diSuccessors, diPredecessors and isDirectPredecessorOf when node is connected to himself. HOT 3
- Constrained User Guide example is not reproducible HOT 3
- Wrong Operations in EqSetFacade HOT 1
- Finding all paths in a graph from node A to node B HOT 2
- Open issues for 2.x HOT 7
- Switch from Travis CI to Github Actions HOT 4
- Enable Scala-Steward on this project HOT 2
- Double (1.0) node is unexpectedly considered as self-successor HOT 9
- type inference not working for .toOuter; Scala 3, scala-graph 1.13.2 HOT 3
- Traversing mutable graphs in multi-thread HOT 4
- Topological sort may not return a node in a cycle when it fails HOT 1
- No potential Scala-Native release? HOT 2
- Documentation issue HOT 1
- persistent data structure HOT 1
- Can't extend Edges in case class in scala 3 HOT 10
- 2 tests failed
- java8 support HOT 2
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-graph.