Hello,
Thank you for your valuable comment and code.
It looks like I made a change that battery is created in ElectricCar
constructor without separate Battery constructor (which indeed was in the book
:)), but as the author said - One day you should consider would you use battery
on its own or
i think i'm looking at a different edition,(pg 174 for me) but the point of the
exercise is adding a separate Battery object to the ElectricCar once there get
too many battery-related variables, and it makes use of default parameters when
initializing the Battery.
so, perhaps nitpicking here, b
Thank you. This idea also showed in my mind… but I wanted to reflect python
code from the book as much as possible. Anyway solution worth knowing.
You could also do something like this, defining a type alias for battery size.
Or `BatterySize = distinct int` if you want a type `BatterySize` that is
distinct from any other int but still uses an int data type. That means you
need to initialize 70 as `70.BatterySize` though, or create a `conve
Thank you for your example. Don't know much about what's happening there, but
I'll investigate it. And yeah, Can't wait to learn templates and macros.
Sorry for introducing a D feature in Nim forum. I am always a fan of simple
programming. If you can do the magic in one line of code, i will call it
simplicity. There are lot of simple things in Nim. Great language with ease of
use. But unfortunately, i am not a fan of macros in Nim. May be beca
You can achieve similar shortcuts in Nim via templates and macros, but please
don't confuse newcomers. ;-)
There is a feature in D.
struct ElectricCar {
int batterySize ;
alias batterySize this ; // This is the magic line
}
// usage
auto ec = ElectricCar() ;
ec = 25 ;// now ec.batterySize will be 25.
Run
You're welcome :)
It works! I'm back on track. Thank you for your answer.
When you initilize your car it expects you to pass in an object of type
`Battery` to the electric car's `battery_size` parameter (because that is what
you defined it to be). So you must wrap your `70` in a `Battery` object like
this:
var c = ElectricCar(year: 2019, battery_size: B
Hi guys,
I'm seriously learning Nim with the help of tutorials, manual and Python Crash
Course (currently page 234 - Instances as Atributes). so i created type
Battery, and type electric Car and try to embeed battery into car so I could
set it when creating new car. But it fails with type misma
12 matches
Mail list logo