Character Type
Conjure offers no dedicated char type because of the optimizations and features performed to support our native string type. Instead, individual characters are represented by explicitly using their integer equivalents.
You can still represent individual characters using single-quotes to denote a character literal, but the resulting type will be sized to the smallest possible unsigned integer value that can represent the character. This means that a character within the ASCII range will result/fit in u8, while an emoji or other Unicode character would result/fit in a u16 or u32.
var letter = 'A' // ASCII character, type is u8
var emoji = '🎮' // Unicode character, type is u32String Type
The string type is a comprehensive, built-in type for text data. They are immutable and UTF-8 encoded by default. Strings are encapsulated in double quotes and support escape sequences. In addition to single-line strings, Conjure supports multi-line strings natively along with string interpolation.
// Single-line string
var message string = "Hello, Conjure!"
// Multiline string
var multiline = "This string can span
multiple lines naturally"
// String interpolation
var name = "Alice"
var greeting1 = "Hello, " + name + "!"
// String interpolation syntax
var greeting2 = "Hello, @{name}!"How Strings Are implemented
Conjure’s built-in string type is relies on string interning for efficiency. This means that identical string literals are stored only once in memory, reducing duplication and improving performance for string comparisons.
Strings in Conjure are immutable, meaning that once a string is created, it cannot be changed. Any operation that modifies a string will result in the creation of a new string.
String Interoperability with C Proposed
Conjure’s string type can be automatically converted to a C-style string when calling external functions by using the c module’s str type. The compiler will handle the conversion by allocating a null-terminated copy of the string in memory, preserving the original string’s immutability.
import "c"
@extern func cFunction(input c.str)
cFunction("Hello from Conjure!")If you wish to hand the C function an unsafe, mutable pointer to the string’s data, you can use the c.strptr type. This will provide a pointer to the internal string data, but you must ensure that the string remains in scope and is not modified while the C code is using it.
import "c"
@extern func cFunction(input c.strptr)Automatic String Localization Proposed
A planned feature of Conjure is automatic detection of strings that may require localization. By marking strings for localization, tools can extract them for translation and manage different language resources.