Add literals
This commit is contained in:
parent
018e91979a
commit
3285bfd0fb
|
@ -0,0 +1,81 @@
|
|||
actor Main
|
||||
new create(env: Env) =>
|
||||
env.out.print("placeholder value")
|
||||
|
||||
// Single quotes for character literals
|
||||
// You can use unnecessarily long ints to store characters, even
|
||||
// though they only require a U8
|
||||
// You need to specify the type, though. Pony cannot infer it
|
||||
let big_a: U8 = 'A'
|
||||
let concrete_type_specified_a: U8 = 'A'
|
||||
let unnecessarily_long_type: U64 = '\n'
|
||||
|
||||
// Pony needs to be told what type to use to store most literals
|
||||
// You can do it like this:
|
||||
let hundred: U8 = 100
|
||||
// Or like this (with a constructor):
|
||||
let a_lot = U16(999)
|
||||
|
||||
// Double quotes or triplicate double quotes for strings
|
||||
// There's only the one string type, so you can let Pony
|
||||
// infer the type of strings
|
||||
let some_string = "p\xF6n\xFF"
|
||||
|
||||
// All string literals support multiline strings:
|
||||
let long_string = "
|
||||
hello
|
||||
there
|
||||
!
|
||||
"
|
||||
|
||||
// Note: String literals contain the actual bytes that were read from
|
||||
// the relevant source file, so their value depends on the encoding
|
||||
// of the source file.
|
||||
|
||||
// Another note! If you use the same String literal lots of times in one
|
||||
// Pony program, they will be stored as the same entry in memory, so
|
||||
// they will always be equal based on identity.
|
||||
|
||||
// Array literals are enclosed by square brackets
|
||||
let my_literal_array =
|
||||
[
|
||||
"an item"
|
||||
"another item"; "yet another (this is like a new line)"
|
||||
]
|
||||
|
||||
// If the type of the array is not specified, the resulting type of the
|
||||
// literal array expresion is Array[T] ref, where T (the type of the
|
||||
// elements) is inferred as the union of all the element types
|
||||
|
||||
// If the variable or call argument the array literal is assigned to
|
||||
// has a type, the literal will be coerced to that type.
|
||||
// Thus this array is coereced to Array[Stringable] ref, which is
|
||||
// possible because Stringable is a trait implemented by both
|
||||
// String and U64.
|
||||
let my_stringable_array: Array[Stringable] ref =
|
||||
[
|
||||
U64(0xA)
|
||||
"0xA"
|
||||
]
|
||||
|
||||
// You can also get a different reference capability than ref for
|
||||
// a literal array just by specifiying it. For example, an array with
|
||||
// reference capability `val` follows
|
||||
let my_val_array: Array[Stringable] val =
|
||||
[
|
||||
U64(0xABAD)
|
||||
"0x1DEA"
|
||||
]
|
||||
|
||||
// You can use an `as` expression to give the compiler a hint as to
|
||||
// what type to coerce the array's elements to
|
||||
// This array gets coerced to Array[Stringable] ref
|
||||
// The `as` expression must follow directly after the opening bracket
|
||||
// of the array literal and be followed by a colon as shown
|
||||
let an_array_with_as =
|
||||
[ as Stringable:
|
||||
U64(0xDEAD)
|
||||
"0xBEEF"
|
||||
U64(1 + 1)
|
||||
]
|
||||
|
Loading…
Reference in New Issue