Giter VIP home page Giter VIP logo

Comments (9)

medvednikov avatar medvednikov commented on July 21, 2024 2

It's a bug.

V doesn't have mutable types (&User vs mut &User).

The checker simply needs to improve to handle this. It already prohibits foo := mutable_ref.

from v.

JalonSolov avatar JalonSolov commented on July 21, 2024 1

It's not a "feature", per se... it is simply the way things work, IMO.

from v.

JalonSolov avatar JalonSolov commented on July 21, 2024 1

However, the code here isn't modifying User or &User. It is only modifying the mutable field inside User. That's why I said it is the way things work.

Creating an immutable reference does not change the mutability of the field.

from v.

felipensp avatar felipensp commented on July 21, 2024

Thoughts @medvednikov @spytheman ?

from v.

JalonSolov avatar JalonSolov commented on July 21, 2024

This is why pointers are so fraught...

Yes, rere returns an immutable pointer... to a struct with a mutable field. Modifying that field is perfectly valid.

from v.

mrtowers avatar mrtowers commented on July 21, 2024

This is why pointers are so fraught...

Yes, rere returns an immutable pointer... to a struct with a mutable field. Modifying that field is perfectly valid.

so that's not a bug? it's a feature?

from v.

ttytm avatar ttytm commented on July 21, 2024

I think it should not be possible to create a mutable reference to an immutable variable:

Same as

i := 0
mut ref := &i

Results in:

error: `i` is immutable, cannot have a mutable reference to it
   20 | 
   21 | i := 0
   22 | mut ref := &i
      |       

from v.

mrtowers avatar mrtowers commented on July 21, 2024

However, the code here isn't modifying User or &User. It is only modifying the mutable field inside User. That's why I said it is the way things work.

Creating an immutable reference does not change the mutability of the field.

V immutable variable isn't JavaScript's const i guess. Fields and variable itself shouldn't be mutable if it's not declared as mut, am i right?

from v.

JalonSolov avatar JalonSolov commented on July 21, 2024

Correct. "Immutable by default." This is opposite most other languages.

from v.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.