Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

https://learnxinyminutes.com/docs/nim/ error in the learning documentation code #23721

Closed
HeadBangZ opened this issue Jun 14, 2024 · 3 comments
Closed

Comments

@HeadBangZ
Copy link

Description

type
  Room = ref object # reference to an object, useful for big objects or
    windows: int    # objects inside objects
    doors: int = 1  # Change the default value of a field (since Nim 2.0)
  House = object
    address: string  
    rooms: seq[Room]

var
  defaultHouse = House() # initialize with default values
  defaultRoom = new Room() # create new instance of ref object
  sesameHouse = House(address: "123 Sesame St.", rooms: @[defaultRoom])

This piece of code produces an error, more specifically it says you can create a new isntance of ref object using

defaultRoom = new Room() --> but this produces an error

Error: type mismatch
Expression: new Room(doors: 1)
  [1] Room(doors: 1): Room

Expected one of (first mismatch at [position]):
[1] proc new(t: typedesc): auto
[1] proc new[T](a: var ref T)
  expression 'Room(doors: 1)' is immutable, not 'var'
[1] proc new[T](a: var ref T; finalizer: proc (x: ref T) {.nimcall.})
  expression 'Room(doors: 1)' is immutable, not 'var'

Nim Version

2.0.4

Current Output

Error: type mismatch
Expression: new Room(doors: 1)
  [1] Room(doors: 1): Room

Expected one of (first mismatch at [position]):
[1] proc new(t: typedesc): auto
[1] proc new[T](a: var ref T)
  expression 'Room(doors: 1)' is immutable, not 'var'
[1] proc new[T](a: var ref T; finalizer: proc (x: ref T) {.nimcall.})
  expression 'Room(doors: 1)' is immutable, not 'var'

Expected Output

Expected compile to be a success

Possible Solution

No response

Additional Information

No response

@beef331
Copy link
Collaborator

beef331 commented Jun 14, 2024

Wrong repo https://github.com/adambard/learnxinyminutes-docs/issues/new

The code should be new Room or better yet Room()

@beef331 beef331 closed this as completed Jun 14, 2024
@HeadBangZ
Copy link
Author

HeadBangZ commented Jun 14, 2024

@beef331 thanks, should I recreate the mistake in the correct repo?

@beef331
Copy link
Collaborator

beef331 commented Jun 14, 2024

Seems they prefer a PR to fix the incorrect code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants