Categories
Haxe

Haxe3 Features: Variable Substitution in Haxe3 (aka String Interpolation)

One of my favourite features in the upcoming Haxe3 is also one of the simplest: String Interpolation (also called variable substitution).  If you were using Std.format() in Haxe2, you’ll recognise it.  It lets you do this:

var name = "Jason";
var age = 25;
trace ('My name is $name, I am $age years old.');
// My name is Jason, I am 25 years old

The first point to make is that it is triggered by using single quotation marks (‘), rather than double (“).  If you use double quotes, everything is treated as a plain string:

trace ("My name is $name, I am $age years old.");
// My name is $name, I am $age years old

The next point to make is that this all happens at compile time.  So the trace we made above, in Javascript, would output as:

console.log("My name is " + name + ", I am " + age + " years old");

So you can only do this with strings you have at compile time, as you’re writing the code.  If you want Strings that are given at runtime to have variable interpolation, you should use a templating library like erazor.

Now a few other things to note:

  1. If you want to put in a normal dollars sign, you can use two $, like this:
    trace ('The item costs $20');
    // "20" is not a valid variable name, so it is ignored.
    // Same as ("The " + item + " costs $20");
    
    trace ('That price is in $USD');
    // Error: Unknown Identifier "USD"
    trace ('That price is in $$USD');
    // Same as ("That price is in $" + "USD");
    
    var cost = 25;
    trace ('That item costs $$$cost');
    // The first two "$" are for the literal sign, the third is part of the variable.
    // Same as ("That item costs $" + cost);
  2. If you want to access anything more than a straight variable, use curly brackets:
    // Property access
    trace ('My name is ${user.name} and I am ${user.age} years old.');
    // Same as: "My name is " + user.name + " and I am " + user.age + " years old.";
    // Outputs: My name is jason and I am 25 years old.
    
    // A simple haxe expression
    trace ('$x + $y = ${x + y}');
    // Same as: "" + x + " + " + y + " = " + (x + y);
    // Outputs: 1 + 2 = 3
    
    // A function call 
    trace ('The closest Int to Pi is ${Math.round(3.14159)}');
    // Same as: "The closest Int to Pi is " + Math.round(3.14159);
    // Outputs: The closest Int to Pi is 3
  3. There is no HTML escaping, so be careful:
    var bold = "<b>Bold</b>";
    trace ('<i>Italic</i> $bold');
    // <i>Italic</i> <b>Bold</b>
    
    var safeBold = StringTools.htmlEscape("<b>Bold</b>");
    trace ('<i>Italic</i> $safeBold');
    // <i>Italic</i> &lt;b&gt;Bold&lt;/b&gt;
    
    var safeEverything = StringTools.htmlEscape('<i>Italic</i> $bold');
    trace (safeEverything);
    // &lt;i&gt;Italic&lt;/i&gt; &lt;b&gt;Bold&lt;/b&gt;

There you have it: String Interpolation, one of the most helpful (and easy to comprehend) features of the upcoming Haxe3 release.

Categories
Faith Personal

Cloudy Mornings

I’ve been using a book called “Common Prayer“, which acts as a kind of guide to help me think of things to pray about and let my mind chew on each morning, lunch time and night.  (Example: when I usually get distracted by what I’ve got on for the day, it says “pray for others”.  Good idea!)

Anyway, each morning the prayers open with this line:

Lord, let me soul rise up to meet you

As the day rises to meet the sun

The last few weeks, I’ve mostly been reading this and praying this as I begin my walk to work, walking down the street, the morning is still cool, but the sun is shining, and I visualise it: the earth reorients itself once again, so my little corner is facing the sun.  Jason, your turn, reorient yourself, turn and face God.  Starting the day this way is good.

This morning however, it wasn’t sunny.  And I came to pray this line, and went to look up at the sun, to help visualise it, and I couldn’t see it.  It was gone.  The clouds had taken it away.

Now of course, the sun wasn’t gone.  It’s still there.  If it were not, it would be pitch black (not just a little grey), the temperature would be dropping so rapidly we would probably have frozen to death by now, and in general things would be falling apart.  I might not be able to see it, and I might be a little chilly, and a little wet with rain, but if the sun were not there, things would be far, far worse than they are.

This is helpful on the days that I feel a little uncomfortable spiritually, can’t see God and struggle to believe he’s there.  Or maybe I’m not struggling with his existence, but just wondering why he’s not doing anything helpful for me with everything I’m struggling with.  On those days, it’s not that God is gone.  He’s still there, and he’s still keeping the general universe running, even if it’s a little obscured, and even if I’m not as comfortable as I want to be.  If he was genuinely not there, or genuinely had ceased taking any interest in me, things would probably be far worse than they are*.

 

* footnote: I’m lucky enough to be healthy and live in a first world country.  My idea of struggles of course aren’t worth even mentioning when compared to what people in different circumstances.  Something else this prayer book is teaching me to be mindful of.  Is God still there for those people?  I hope so…