literal

Dart: How to return a known, constant, or literal value in a Future

Sometimes a Dart or Flutter API may require you to return a Future even if you already have a known, constant, or literal value. If/when that happens, you can use this approach:

return new Future(() { return 42; });

In this example the known value is 42, but it could have been a string like "Hello", a boolean like true, or any other known or literal value.

Scala: How to create XML literals

Problem: You want to create XML variables, and embed XML into your Scala code.

Solution

You can assign XML expressions directly to variables, as shown in these examples:

val hello = <p>Hello, world</p>
val p = <person><name>Edward</name><age>42</age></person>

In the REPL you can see that these variables are of type scala.xml.Elem:

Scala XML examples: XML literals, mixing XML and Scala source code, XPath searching

A really terrific feature about Scala is that XML handling is built into the language. This means you don't have to deal with XML as String objects, you deal with it as XML objects.

Here are just a few examples of using XML in Scala. First, you can create an XML literal like this:

scala> val hello = <p>Hello, world</p>
hello: scala.xml.Elem = <p>Hello, world</p>

Again, note that this is not a String, there are no double quotes; we've just defined an XML literal in Scala.