Comments (2)
I don't think this is a desirable fix for this problem. Python allows all sorts of weird getitem/setitem things, and Mojo doesn't want to impose "taste" (or even "common sense" :-) ) on library authors.
The issue here is that referring to the alias value "materializes" it into a local, so the code is like:
fn main():
alias foo = S(0)
var tmp1 = foo
tmp1[] = 10
var tmp2 = foo
print(tmp2[]) # prints 0
The behavior is a bit surprising but stacks out from how other parts of the language works.
from mojo.
@lattner But the following code doesn't compile:
@value
struct S:
var data: Int
fn __setitem__(inout self, value: Int):
self.data = value
fn main():
alias foo = S(0) # error: invalid call to '__setitem__': invalid use of mutating method on rvalue of type 'S'
foo[] = 10
print(foo[]) # prints 0
from mojo.
Related Issues (20)
- [Docs] Suggestion: link to python types docs in module | Dict
- [BUG]Mojo doesnt recognise main function HOT 5
- [BUG] Errors with optional recursive struct definitions HOT 1
- [Feature Request] Make `StringRef.find` call `StringSlice.find`
- [BUG] [stdlib] Fix `String.format()` to use explicit byte indexing and not assume `String.__getitem__` indexes directly to byte offset.
- [BUG] Mojo SDK post-install test failures
- [BUG] Mojo SDK post-install test failures
- [BUG] Jupyter Notebook fails to load mojo kernel
- [Feature Request] Add functionality like `@classmethod` to enforce conditional conformance on struct static methods
- [Docs] Mojo URL leads to 404 HOT 2
- [BUG] Type checker is failing to compare two function types HOT 1
- [BUG] global variable initialized from num_performance_cores() become 0 after compile HOT 5
- [BUG] `parallelize` should abort if num_workers < 0, currently it has weird behavior HOT 2
- [Modular CLI] mojo-nightly.path missing from modular.cfg post successfull install
- [BUG] Top level `var` segfaults when used from package HOT 4
- [BUG] `print(None)` doesn't work anymore
- [BUG] `0/0 != 1` returns `False` HOT 2
- [BUG] Compiler crash with (indirect) self referential type
- [Feature Request] Add `__name__`
- [BUG] Working LinkedList using pointers, but debug is failing.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mojo.