Closed salmaner closed 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.)
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]
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.
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!
Fixed.
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:
echo comp