Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Understand Evan's example #2

Open
DanielSank opened this issue Jan 21, 2016 · 4 comments
Open

Understand Evan's example #2

DanielSank opened this issue Jan 21, 2016 · 4 comments

Comments

@DanielSank
Copy link
Owner

I'm not sure what this is for yet, but Evan said it would be helpful :D

import scala.language.experimental.macros
import scala.reflect.runtime.universe._

object Hi {

 def interrogated(strArg: Array[String], intArg: Int, str2: String) : Int = 4

 def paramInfo[T](x: T)(implicit tag: TypeTag[T]): Unit = {
     val targs = tag.tpe match { case TypeRef(_, _, args) => args }
     println(s"type of $x has type arguments $targs")
 }

 def main(args: Array[String]) = paramInfo(interrogated _)
}
@lovesegfault
Copy link
Contributor

  1. Who's Evan?
  2. Do we know what is it useful for yet?
  3. Scala looks menacing

@DanielSank
Copy link
Owner Author

Who's Evan?

Friend of mine.

Do we know what is it useful for yet?

The problem comes down to doing this:

def call_target_method_with_data(bytes, method):
    args = somehow_parse_bytes_into_python_objects(bytes)
    method(*args)

in a staticly typed language. That *args bit is nontrivial to accomplish and is, as far as I can tell, the most technically tricky bit of doing RPC in static languages.

Scala looks menacing

No way, it's awesome! I started learning it during the Christmas break and already found it pretty intuitive in many ways. As with most languages, the hardest part is learning how to set up a build system, but you're in luck because I made a repo with some examples and instructions for how to get started. I'd be very happy to add explanatory notes etc. to that repo. Pull requests are most welcome.

@lovesegfault
Copy link
Contributor

somehow_parse_bytes_into_python_objects(bytes)
Best function name ever. Can we use pure magic to implement it?

Cool, Scala examples! I'll set up the dev environment on my work PC and start tinkering with it.

@DanielSank
Copy link
Owner Author

somehow_parse_bytes_into_python_objects(bytes)

Best function name ever. Can we use pure magic to implement it?

Doing the conversion is actually not that hard, even in a language with static types. Suppose my wire format is something like

<method name as a string>:<text representation of floating point number>

Then I can parse the bytes like this:

def somehow_parse_bytes_into_python_objects(bytes):
    """Get the method name and argument from an array of bytes.

    Args:
        bytes (str): Byte string which came in over the wire, i.e. via TCP
            or whatever. Note that in python 2 a str is really just an array
            of bytes.
    """
    separator_index = bytes.find(':')
    method_name_bytes = bytes[0:separator_index]
    arg_bytes = bytes[separator_index:]
    arg = float(arg_bytes)
    return method_name, arg

In a static language you can do the conversion from text to float too, that's not python specific at all.

Cool, Scala examples! I'll set up the dev environment on my work PC and start tinkering with it.

Yeah go for it. The examples all work but they're not particularly well explained. I bet you can figure out most of it. Search online for "Scala by Example" for a somewhat nice introduction.

Now, beware: a lot of Scala tutorials focus rather heavily on the functional aspects of the language. That's all well and good, but to a large extent Scala is an object oriented language which is just a hell of a lot nicer to use than Java for a lot of reasons. Don't get too hung up on monads and folding etc. etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants