nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.22k stars 1.46k forks source link

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

Closed HeadBangZ closed 2 weeks ago

HeadBangZ commented 2 weeks ago

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 commented 2 weeks ago

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

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

HeadBangZ commented 2 weeks ago

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

beef331 commented 2 weeks ago

Seems they prefer a PR to fix the incorrect code.