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 mismatch: got <int literal(70)> but expected 'Battery = object' Could anyone tell me how it should look properly? Here is the code:
https://play.nim-lang.org/#ix=2RTq
Thank you
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: Battery(battery_size: 70))
An object with only an int is not the same as an int from a type point of view :)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.
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 converter intToBatterySize(x: int): BatterySize = x.BatterySize.
type
BatterySize = int
ElectricCar = object
year: int
batterySize: BatterySize
var c = ElectricCar(year: 2019, batterySize: 70)
echo c.year
echo c.batterySize
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, but a direct translation from python would be:
type
Car = ref object of RootObj
make,model:string
year:int
ElectricCar = ref object of Car
battery_size:int
#equivalent to __init__(self,make,model,year)
proc newElectricCar(make,model:string,year:int):ElectricCar =
ElectricCar(make:make,mode:model,year:year,battery_size:70)
now ElectricCar gets updated to include a new Battery class:
type
ElectricCar = ref object of Car
battery:Battery
Battery = object
battery_size:int
#battery '_init_' with default parameter
proc initBattery(size = 70):Battery = Battery(battery_size:size)
proc describe_battery(self:Battery) =
## Print a statement describing the battery size.
echo "This car has a " & $(self.battery_size) & "-kWh battery."
#updated ElectricCar '__init__':
proc newElectricCar(make,model:string,year:int):ElectricCar =
ElectricCar(make:make,model:model,year:year,battery:initBattery())
let mytesla = newElectricCar("tesla","model s",2016)
mytesla.battery.describe_battery()
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 make it part of a car. Anyway our code looks similar although yours feels simpler...
https://play.nim-lang.org/#ix=2Sak
Good material to study.
Cheers,