Search code examples
transcrypt

Transcrypt and TranscryptFrame


Right now I am working on a wrapper for common functions for transcrypt called TranscryptFrame. I get a compiler error and I like to know if we can solve this in any way, maybe it is just a little change in the code to get things working.

You need to have the latest TranscryptFrame.py file in the working directory for compiling. You find it here: https://github.com/bunkahle/Transcrypt-Examples/blob/master/dom/TranscryptFrame.py - please feel free to suggest changes or improvements on this file or bug reports. It might be available sometime as a library in the future.

So here is the code which doesn't even compile, I get the error can't assign to function call.

# change_text2a_tf.py
import TranscryptFrame as tf

def insert():
    # working:
    # myElement = tf.S("#intro")
    # tf.S("#demo").innerHTML = "The text from the intro paragraph is " + myElement.innerHTML

    # working:
    # myElement_htm = tf.S("#intro", "htm")
    # tf.S("#demo").innerHTML = "The text from the intro paragraph is " + myElement_htm

    # working:
    # tf.S("#demo").innerHTML = "The text from the intro paragraph is " + tf.S("#intro", "htm")

    # not working: can't assign to function call on compiling
    tf.S("#demo", "htm") = "The text from the intro paragraph is " + tf.S("#intro", "htm")

and this is the html for running it:

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="__javascript__/change_text2a_tf.js"></script>
    <title>Change Text with TranscryptFrame</title>
  </head>
  <body onload=change_text2a_tf.insert()>
    <p id="intro"><b>Hello World!</b></p>
    <p>This example demonstrates the <b>getElementById</b> method!</p>
    <p id="demo"></p>
  </body>
</html>

Solution

  • In the line:

    tf.S("#demo", "htm") = "The text from the intro paragraph is " + tf.S("#intro", "htm")
    

    you are assigning something to the result of a function, namely to tf.S("#demo", "htm")

    This isn't possible in Python, JavaScript or Transcrypt. It is possible in C++ by returning a reference, but C++ is rather unique in that.

    A function in Python, JavaScript or Transcrypt does not return an LValue (location in memory) but an RValue (the value that resides in that location). You can't store anything in a value, only in a location.

    The following piece of C++ code uses references and a function call to the left of a =:

    float &lowestGrade (float &x, float & y) {
        return x < y ? x : y;
    }
    

    It can be called as follows:

    grade1 = 4;
    grade2 = 6;
    lowestGrade  (grade1, grade2) += 2;
    

    Note that grade1 will be 6 after this.

    But as said, functions returning LValues (locations in memory) are rather unique for C++.