Complex example
Hi, I just love your framework (finally I don't see myself lost coming from C# and Swift worlds), but is there any complex example? I'm currently looking for these: 1] How do you create collections of class objects? 2] How to have class properties which are based on other classes? I mean:
class:Location() {
public string x
public string y
}
class:Vehicle() {
public Location location
public string name
}
Is it possible at all?
Thank you.
Maybe you could check the option class I created, it has an Options collection: https://github.com/niieani/bash-oo-framework/blob/d71a74aad77a1bfcdaff032b81090dfd1507e221/lib/util/option.sh
https://github.com/niieani/bash-oo-framework/blob/d71a74aad77a1bfcdaff032b81090dfd1507e221/example/option.sh
@constrict0r, that's some hefty work indeed. Pretty cool! @rbukovansky to answer your questions:
- creating collections of class instances is a bit tricky for now, but you can capture the definitions of instances (result of
declare -p YOUR_VARIABLEand store them in a normal Array). The instance is sort of "serialized" that way. - second is possible and should just work as long as you initialize your type before defining the other class. If it doesn't happen, feel free to open an issue, as it's a bug.
@constrict0r Thanks, but I'm not sure, which question your answer answers. 🤷🏻♂️ Could you please elaborate a little bit more? Thank you!
@niieani So, you mean I need to:
- Create Class Location
- Create an object of class Location
- Set its properties
- Create Class Vehicle
- Create an object of class Vehicle
@rbukovansky , something like this?:
source "$( cd "${BASH_SOURCE[0]%/*}" && cd .. && pwd )/lib/oo-bootstrap.sh"
import util/class
class:Location() {
public string x
public string y
}
Type::Initialize Location
class:Vehicle() {
public Location location
public string name
Vehicle.getter() {
@return:value $(this location toString)
}
Vehicle.Set() {
[reference] toSet
this location x = $($var:toSet x)
this location y = $($var:toSet y)
}
}
Type::Initialize Vehicle
string xSample=33
string ySample=55
Location locationSample
$var:locationSample x = $xSample
$var:locationSample y = $ySample
echo '--------------'
$var:locationSample
Vehicle vehicleSample
$var:vehicleSample Set locationSample
echo '--------------'
$var:vehicleSample
Results:
([__object_type]="Location" [x]="33" [y]="55" )
([location]="([0]=\"([__object_type]=\\\"Location\\\" [x]=\\\"33\\\" [y]=\\\"55\\\" )\")" [__object_type]="Vehicle" )
@constrict0r Holy crackers! 😮 Thanks a lot! 👍