Numeric literals can be used to encode any signed or unsigned integer or floating point number.
In most cases Pony is able to infer the concrete type of the literal from the context where it is used (e.g. assignment to a field or local variable or as argument to a method/behaviour call).
It is possible to help the compiler determine the concrete type of the literal using a constructor of one of the numeric types:
Character literals are enclosed with single quotes (').
Character literals, unlike String literals, encode to a single numeric value. Usually this is a single byte, a U8. But they can be coerced to any integer type:
It is possible to have character literals that contain multiple characters. The resulting integer value is constructed byte by byte with each character representing a single byte in the resulting integer, the last character being the least significant byte:
String Literals contain the bytes that were read from their source code file. Their actual value thus depends on the encoding of their source.
Consider the following example:
letu_umlaut="ü"
If the file containing this code is encoded as UTF-8 the byte-value of u_umlaut will be: \xc3\xbc. If the file is encoded with ISO-8559-1 (Latin-1) its value will be \xfc.
For embedding multi-line text in string literals, there are triple quoted strings.
lettriple_quoted_string_docs=""" Triple quoted strings are the way to go for long multi-line text. They are extensively used as docstrings which are turned into api documentation. They get some special treatment, in order to keep Pony code readable: * The string literal starts on the line after the opening triple quote. * Common indentation is removed from the string literal so it can be conveniently aligned with the enclosing indentation e.g. each line of this literal will get its first two whitespaces removed * Whitespace after the opening and before the closing triple quote will be removed as well. The first line will be completely removed if it only contains whitespace. e.g. this strings first character is `T` not `\n`. """
When a single string literal is used several times in your Pony program, all of them will be converted to a single common instance. This means they will always be equal based on identity.
letpony="🐎"letanother_pony="🐎"ifponyisanother_ponythen// True, therefore this line will run.end
If the type of the array is not specified, the resulting type of the literal array expression is Array[T] ref where T (the type of the elements) is inferred as the union of all the element types:
It is also possible to give the literal a hint on what kind of type it should coerce the array elements to using an as Expression. The expression with the desired array element type needs to be added right after the opening square bracket, delimited by a colon:
Constructing an array with a literal creates new references to its elements. Thus, to be 100% technically correct, array literal elements are inferred to be the alias of the actual element type. If all elements are of type T the array literal will be inferred as Array[T!] ref that is as an array of aliases of the type T.
It is thus necessary to use elements that can have more than one reference of the same type (e.g. types with val or ref capability) or use ephemeral types for other capabilities (as returned from constructors or the consume expression).