Replies: 1 comment
-
|
You've hit on a genuinely frustrating and surprisingly opaque aspect of the Tour/Spec/Docs for the Mojo language regarding dictionary (dict) syntax. You're absolutely right to be confused – it's inconsistent and poorly explained. You've articulated the problem perfectly. Here's a breakdown of what's going on, based on digging through the available information (and a lot of trial and error), and why it's so confusing: The Core Issue: Two Syntaxes for the Same Thing Mojo allows both = and : for defining key-value pairs within a dictionary literal. This is a deliberate design choice, stemming from Mojo's attempt to bridge the gap between Python-like readability and systems-level control. What the = Sign Means: Attribute Assignment: The = sign is primarily used for assigning values to attributes of objects. However, within a dictionary literal, it's also interpreted as a key-value pair definition. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
It's confusing that dict key-values can be declared with either
:or=and even mixed:The tour uses examples with
=, while the spec uses examples with:. But none of the docs really states the formal syntax, mentions the interchangeability, or states the preferred/recommended syntax.In the code style section, there is the mixed example:
Here the colon
:is referred to dict "instantiation" and equality=referred to as "override attribute operator". But it is unclear whether the difference is syntactical sugar or is functionally different.Beta Was this translation helpful? Give feedback.
All reactions