Conditional import?

bismuth

New member
The import command works wonders in keeping my .ash code from being duplicated, but it raises a new problem: repeated includes (imports)!

Suppose I have one function library for some generic item handling called "func.ash"
now I make some functions for drinking my fill, checking drunkennes, etc. I call it "drinking.ash" and because it handles items it starts with the line:
import <func.ash>;

I also have some functions for eating, found in "eating.ash" these functions also handle items so the file also starts with the same import line.
All is well, code duplication is minimized, whoohoo!

But now I want to make a new script that does all the stuff I do at the start of a day, i.e.:
- Eat
- Drink
- Buff
- maybe plant some mushrooms
- etc.
because I want to eat and drink in these functions I start with:
import <eating.ash>
import <drinking.ash>


and this is where it all falls down, because here kolmafia complains: "function "[first function in func.ash]" already defined at line X in file "func.ash". debug log printed.

Now in C I'd simply wrap all files in an
#ifndef SOME_UNIQUE_NAME
#defint SOME_UNIQUE_NAME

..
file contents
..

#endif

i.e. a conditional import.
Is there an equivalent for .ash files and if so, what is it?
 

Tirian

Member
At the moment, there is nothing #ifndef-ish. I actually asked for that just a few days ago in the developer forums, but haven't heard or seen anything on the front. To defend the developers, Veracity is putting some awesome effort into clearing up all of the underlying quirkiness in ASH's framework code, so I don't mind being ignored for a little while. :D

To be precise, what I asked for is a little simpler for us and them, since ASH doesn't seem to have (or need) anything like package scope or namespace. Namely, I suggested that the import statement own a list of all of the files that it had imported and skip over any duplicates. I'd write it myself, except that I don't even particularly know if they have time to merge in changes submitted from left field.

In the meantime, I solve this problem by only having the top-level script do the imports, and the libraries that require other libraries just have a whopping big comment saying that you need to import foo.ash if you aren't already. That's not a very good solution, because it means that you couldn't have dual-purpose scripts. I mean that if you had a script that did all the trading with the bounty hunter that required another low-level library, you could either have that be an "executable" script OR you could have that script be a library that could be called by a broader Ice Peak farming script, but you couldn't do both at the moment.
 

bismuth

New member
Well, the "only import in executable scripts" trick is fine for my purposes.
What I'll do is make one script called "functionlibrary.ash" or something that imports all my files and import that into all my executable scripts.
thanks for the reply!
 
Top