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 >.
5 Replies
gabrieldemarmiesse
It's actually the python syntax, mojo isn't inventing anything here: https://peps.python.org/pep-0695/ and https://peps.python.org/pep-0718/
PEP 695 – Type Parameter Syntax | peps.python.org
Python Enhancement Proposals (PEPs)
Jimmy Smith Jr.
Jimmy Smith Jr.OP11mo ago
I see. I wasn't aware that python had such proposals. Thanks for the answer
1
110mo ago
i've just realized thats why i keep writing list(int) and it doesnt work yeah python should ve made it <>
sora
sora10mo ago
Could do clarify what does that refers to in this sentence?
1
110mo ago
i kept writing list(int) and it doesnt say error but type hinting didnt work and i keep forgetting if it is list(int) or list[int]
Want results from more Discord servers?
Add your server