Code Monkey home page Code Monkey logo

Comments (6)

peter-empen avatar peter-empen commented on September 27, 2024

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.

glebashnik avatar glebashnik commented on September 27, 2024

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.

glebashnik avatar glebashnik commented on September 27, 2024

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.

peter-empen avatar peter-empen commented on September 27, 2024

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.

peter-empen avatar peter-empen commented on September 27, 2024

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.

glebashnik avatar glebashnik commented on September 27, 2024

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)

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.