Github user ankurdave commented on the pull request:

    https://github.com/apache/spark/pull/901#issuecomment-44745827
  
    Unlike Python (but like Java), Scala doesn't use asInstanceOf for arbitrary 
type conversions. In this case, it won't work to do `"123".asInstanceOf[Int]`, 
because Scala doesn't know how to do the conversion. There are two ways to 
resolve this:
    
    1. Have `edgeListFile` take a function that does the conversion:
    
        ```scala
        def edgeListFile[ED: ClassTag](..., parseEdgeAttr: String => ED) = {
          ... parseEdgeAttr(lineArray(2)) ...
        }
        // Can be called like this:
        edgeListFile[Int](..., _.toInt)
        ```
    
        This is simple, but it doesn't facilitate reuse of the parsing 
functions. You always have to specify how to parse an Int, even though it's 
usually the same everywhere.
    
    2. Define a library of standard parsers and use Scala implicits to select 
the right one automatically if it exists:
        ```scala
        // `CanParse[T]` is a type class [1] indicating that we can parse any 
String
        // to construct a T.  If there is an `implicit val` of type 
Parseable[Foo]
        // in scope, it means you can convert from any String to a Foo. (This is
        // called Read in Haskell.)
        trait Parseable[T] { def parse(s: String): T }
    
        // Here are some parsers. The user can define their own by creating
        // additional implicit vals.
        object Parseable {
          implicit val IntParser = new Parseable[Int] { def parse(s: String) = 
s.toInt }
          implicit val StringParser = new Parseable[String] { def parse(s: 
String) = s }
        }
    
        // Instead of an explicit function parameter, we use a context bound, 
which
        // desugars to an implicit parameter of type Parseable[ED] (that is, a
        // parser for ED) that we can access using implicitly[Parseable[ED]].
        def edgeListFile[ED: ClassTag : Parseable](...) = {
          ... implicitly[Parseable[ED]].parse(lineArray(2)) ...
        }
        // Can be called without passing anything extra:
        edgeListFile[Int](...)
       ```
    
    ---
    
    Spark does have a [style 
guide](https://cwiki.apache.org/confluence/display/SPARK/Spark+Code+Style+Guide),
 but it's pretty sparse. Most questions will require looking for other examples 
in the code, or just submitting the PR and seeing what reviewers say.
    
    [1] 
http://docs.scala-lang.org/tutorials/FAQ/finding-implicits.html#context_bounds


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to