LiteralTerm constructor

const LiteralTerm(
  1. String value, {
  2. IriTerm? datatype,
  3. String? language,
})

Creates a literal with an optional datatype or language tag

This is the primary constructor for creating RDF literals. It handles the complex rules of the RDF 1.1 specification regarding datatypes and language tags:

  • If datatype is provided, it is used as the literal's datatype
  • If language is provided but no datatype, rdf:langString is used automatically
  • If neither datatype nor language is provided, xsd:string is used by default

According to the RDF 1.1 specification:

  • A literal with a language tag must use rdf:langString datatype
  • A literal with rdf:langString datatype must have a language tag

This constructor enforces these constraints with an assertion.

Example:

// Simple string literal (implicit xsd:string datatype)
final plainLiteral = LiteralTerm('Hello');

// Typed literal
final intLiteral = LiteralTerm('42', datatype: Xsd.integer);

// Language-tagged string (implicit rdf:langString datatype)
final langLiteral = LiteralTerm('Bonjour', language: 'fr');

Implementation

const LiteralTerm(this.value, {IriTerm? datatype, this.language})
    : assert(
        datatype == null ||
            (language == null && datatype != Rdf.langString) ||
            (language != null && datatype == Rdf.langString),
        'Language-tagged literals must use rdf:langString datatype, and rdf:langString must have a language tag',
      ),
      datatype = datatype != null
          ? datatype
          : (language == null ? Xsd.string : Rdf.langString);