Mojo struct
SourceLocation
@register_passable(trivial)
struct SourceLocation
Type to carry file name, line, and column information.
This struct stores source location data and provides utilities for formatting location-prefixed messages, which is useful for error reporting and debugging.
Example:
from reflection import source_location, SourceLocation
fn main():
# Get current location
var loc = source_location()
print(loc) # Prints: /path/to/file.mojo:6:19
# Use prefix() for error-style messages
print(loc.prefix("something went wrong"))
# Prints: At /path/to/file.mojo:6:19: something went wrong
# Access individual fields
print("File:", loc.file_name)
print("Line:", loc.line)
print("Column:", loc.col)Fields
- line (
Int): The line number (1-indexed). - col (
Int): The column number (1-indexed). - file_name (
StaticString): The file name.
Implemented traits
AnyType,
Copyable,
ImplicitlyCopyable,
ImplicitlyDestructible,
Movable,
Stringable,
Writable
comptime members
__copyinit__is_trivial
comptime __copyinit__is_trivial = True
__del__is_trivial
comptime __del__is_trivial = True
__moveinit__is_trivial
comptime __moveinit__is_trivial = True
Methods
__str__
__str__(self) -> String
Returns a string representation of the source location.
Returns:
String: A string in the format "file_name:line:col".
prefix
prefix[T: Writable](self, msg: T) -> String
Returns the given message prefixed with the source location.
Parameters:
- T (
Writable): The type of the message.
Args:
- msg (
T): The message to attach the prefix to.
Returns:
String: A string in the format "At file:line:col: msg".
write_to
write_to(self, mut writer: T)
Formats the source location to the provided Writer.
Args:
- writer (
T): The object to write to.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!