bash-oo-framework icon indicating copy to clipboard operation
bash-oo-framework copied to clipboard

Complex example

Open rbukovansky opened this issue 7 years ago • 5 comments

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.

rbukovansky avatar Jun 18 '18 09:06 rbukovansky

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 avatar Aug 13 '18 16:08 constrict0r

@constrict0r, that's some hefty work indeed. Pretty cool! @rbukovansky to answer your questions:

  1. creating collections of class instances is a bit tricky for now, but you can capture the definitions of instances (result of declare -p YOUR_VARIABLE and store them in a normal Array). The instance is sort of "serialized" that way.
  2. 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.

niieani avatar Aug 13 '18 17:08 niieani

@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:

  1. Create Class Location
  2. Create an object of class Location
  3. Set its properties
  4. Create Class Vehicle
  5. Create an object of class Vehicle

rbukovansky avatar Aug 14 '18 08:08 rbukovansky

@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 avatar Aug 14 '18 19:08 constrict0r

@constrict0r Holy crackers! 😮 Thanks a lot! 👍

rbukovansky avatar Aug 15 '18 12:08 rbukovansky