Giter VIP home page Giter VIP logo

Comments (5)

salmaner avatar salmaner commented on July 26, 2024 1

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<name>\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!

from nimprogrammingbook.

StefanSalewski avatar StefanSalewski commented on July 26, 2024

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.)

from nimprogrammingbook.

StefanSalewski avatar StefanSalewski commented on July 26, 2024

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]

from nimprogrammingbook.

StefanSalewski avatar StefanSalewski commented on July 26, 2024

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.

from nimprogrammingbook.

StefanSalewski avatar StefanSalewski commented on July 26, 2024

Fixed.

from nimprogrammingbook.

Related Issues (17)

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.