StefanSalewski / NimProgrammingBook

Computer Programming with the Nim Programming Language -- A gentle Introduction
259 stars 19 forks source link

Result of some code is not expected as point in the book #30

Closed salmaner closed 8 months ago

salmaner commented 8 months ago

Hello Stefan!

The book is so great! Thank you a lot for that work! I am reading your book now, on chapter Objects, there are code:

Starting with Nim v2.0, object fields can have custom default values, instead of the binary zero. The syntax for the defaults is the same as the assignment for ordinary variables, as shown below:


type
Computer = object
freeShipping: bool = true
manufacturer: string  = "bananas"
cpu: string
powerConsumption: float
ram: int # GB
ssd: int # GB
quantity: int
price: float
var 
comp: Computer
with comp:  
cpu = "Intel i7"
powerConsumption = 17
ram = 32
ssd = 1024
quantity = 3
price = 499.99

echo comp


But it doesn't work as I am expected. 
Two fields have default values, but when I create **comp** the fields **freeShipping** and **manufacturer** is empty. 

> [Running] nim compile --verbosity:0 --hints:off --run "c:\Users\<user>\source\nimProjct\StefanSalewski\main.nim"
> c:\Users\<user>\source\nimProjct\StefanSalewski\main.nim(3, 11) 
> (**freeShipping: false, manufacturer: ""**, cpu: "Intel i7", powerConsumption: 17.0, ram: 32, ssd: 1024, quantity: 3, price: 499.99)
> 
I am testing programming via **Nim** on Windows 10. 
StefanSalewski commented 8 months ago

Thanks for reporting.

Actually, I think I never tested the combination of the new default object values introduced for Nim 2.0 in conjunction with the std/with package. I will test it soon. Please let us know your Nim compiler version (execute "nim -v" in a terminal window.)

StefanSalewski commented 8 months ago

You are right! And it is not related to std/with package.

For $ nim -v Nim Compiler Version 2.1.1 [Linux: amd64] Compiled at 2023-09-13

the default values for objects are only used, when the object variable is created with Nim's constructor syntax as in

#import std/with

type
  Computer = object
    freeShipping: bool = true
    manufacturer: string  = "bananas"
    cpu: string
    powerConsumption: float
    ram: int # GB
    ssd: int # GB
    quantity: int = 7
    price: float = 1000.0

proc main =

  var 
      #comp: Computer # no defaults!
      #comp = Computer()
      comp = Computer(cpu: "AMD Skylake")

  echo comp
#with comp:  
#    cpu = "Intel i7"
#    powerConsumption = 17
#    ram = 32
#    ssd = 1024
#    quantity = 3
#    price = 499.99

#echo comp

main()

Both comp = Computer() and comp = Computer(cpu: "AMD Skylake") works.

Actually I missed that fact, I will make this more clear in the book. I am currently not sure if this behaviour is intended. I guess Mr. Rumpf has good reasons for this behaviour.

Also note, that Nim 2.0 now has the new experimental pragma {.experimental: "strictDefs".}, see https://nimprogrammingbook.com/book/nimprogramming_rouge_molokai.html#_strictdefs

When applying that pragma, the compiler shows a warning for a plain comp: Computer

/tmp/hhh/t.nim(22, 8) Warning: use explicit initialization of 'comp' for clarity [Uninit]
StefanSalewski commented 8 months ago

Yes, from the current version of the language manual we learn that for a plain variable declaration like "comp: Computer" the defaults are not used: https://nim-lang.github.io/Nim/manual.html#types-default-values-for-object-fields

I will try to fix that soon in the book.

salmaner commented 8 months ago

Hello Stefan!

Thanks for your anwer! Sorry for my hesitating with respond. The information of version my Nim compiler:

Nim Compiler Version 2.0.0 [Windows: amd64] Compiled at 2023-08-01 Copyright (c) 2006-2023 by Andreas Rumpf

I am using the scoop for many apps.

C:\Users\\source\nimProjct\StefanSalewski>scoop info nim Name : nim Description : A statically typed compiled systems programming language, which combines successful concepts from mature languages like Python, Ada and Modula. Version : 2.0.0 Bucket : main Website : https://nim-lang.org License : MIT Updated at : 31.08.2023 13:25:07 Updated by : Rashil Gandhi Installed : 2.0.0 Binaries : bin\nim.exe | bin\nimble.exe | bin\nimgrab.exe | bin\nimgrep.exe | bin\nimpretty.exe | bin\nimsuggest.exe | bin\vccexe.exe | bin\testament.exe Suggestions : mingw-winlibs

Although, I use MSYS2 default version MSYS2 UCRT64. According to manual on https://www.msys2.org/docs/environments/ there are recommendations :

UCRT (Universal C Runtime) is a newer version which is also used by Microsoft Visual Studio by default. It should work and behave as if the code was compiled with MSVC.

So, I guess we will expected new version of Nim for windows soon ... :-)

Best regards!

StefanSalewski commented 8 months ago

Fixed.