Jimmy Smith Jr.
Jimmy Smith Jr.
MModular
Created by Jimmy Smith Jr. on 1/8/2024 in #questions
Was there any specific reason for the current template syntax?
Mojo currently uses the [ and ] characters to specify templates instead of the < and > characters that we see in most other languages. Initially one would think it is to avoid the known ambiguity the latter characters can cause, however the current characters also make the grammar ambiguous.
fn function[t: Int]() -> Int:
return t


fn main():
let x = function[0]() # function call
fn function[t: Int]() -> Int:
return t


fn main():
let x = function[0]() # function call
@value
struct object[t: Int]:
pass


fn main():
let y = object[0]() # struct instantiation
@value
struct object[t: Int]:
pass


fn main():
let y = object[0]() # struct instantiation
from collections.vector import DynamicVector


@value
struct callable(CollectionElement):
fn __call__(self) -> Int:
return 1


fn main():
let vector = DynamicVector[callable]()
let z = vector[0]() # member access + call
from collections.vector import DynamicVector


@value
struct callable(CollectionElement):
fn __call__(self) -> Int:
return 1


fn main():
let vector = DynamicVector[callable]()
let z = vector[0]() # member access + call
In all of these cases the syntax is identifier[literal](), but every case parses into a different AST. So I wonder if there was any specific reason for choosing the current syntax or it was only for using different tokens than < and >.
8 replies