diff --git a/README.md b/README.md index 5302f0e..ebb9835 100644 --- a/README.md +++ b/README.md @@ -92,14 +92,12 @@ Example.Object obj1 "Test" You can then call it's functions. ```bash $obj1.print -obj1.print ``` -**NOTE**: The $ is not mandatory, but is recommanded for clarity. ... or access and edit it's properties. ```bash -name=$(obj1.name) -obj1.name = "New name" +name=$($obj1.name) +$obj1.name = "New name" ``` You can store objects in variables as a string. For example, you can have have objects as class arguments, function returs or arrays of objects like this: diff --git a/oop.sh b/oop.sh index 6df8ba5..fe493b4 100644 --- a/oop.sh +++ b/oop.sh @@ -1,6 +1,6 @@ # # BashOOP - Simple OOP implementation for bash. -# Copyright (C) 2021 Ad5001 +# Copyright (C) 2022 Ad5001 # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by @@ -24,6 +24,12 @@ _namespace="" _namespacePath=$(realpath $(dirname ${BASH_SOURCE[0]})) +# Allows the creation of temporary, unique names for variables so that +# when declaring a new object with the same variable name, the previous one wouldn't be overwritten with +# the new object. +# However, this disallows the syntax that doesn't use $ at first, which makes the documentation clearer. +_counter=0 + # This dictionnary saves all classes for each namespace so they can be retreived and aliased. declare -Ag _namespacesClasses # This dictionnary links all files for static namespaces. @@ -93,17 +99,21 @@ _createObject() { varName=$3 constructorArguments="${@:4}" + # Get a temporary variable. It's equivalent to the memory space in bash of the object. + varTmpName="_obj${_counter}" + _counter=$((_counter + 1)) + # Declare dummy constructor. - eval "$varName.constructor() { :; }" + eval "$varTmpName.constructor() { :; }" # Declare base properties. - eval "$varName.type() { echo $type; }" - eval "$varName.source() { echo $associatedFile; }" + eval "$varTmpName.type() { echo $type; }" + eval "$varTmpName.source() { echo $associatedFile; }" # Create property array. - createPropertyHolder $varName - # alias the "varName" variable to itself, so that it can be used and transmitted in other variables (e.g: $varName.name would alias to varName.name) - eval "$varName='$varName'" + createPropertyHolder $varTmpName + # alias the "varTmpName" variable to itself, so that it can be used and transmitted in other variables (e.g: $varName.name would alias to varTmpName.name) + eval "$varName='$varTmpName'" # Imports the file and replace all "." with the variable name. - . <(sed s/this\\./$varName./g <(sed s/$type\\./$varName./g $associatedFile)) + . <(sed s/this\\./$varTmpName./g <(sed s/$type\\./$varTmpName./g $associatedFile)) # Call the constructor $varName.constructor $constructorArguments }