Author Topic: Reuse of code/classes?  (Read 3910 times)

0 Members and 1 Guest are viewing this topic.

Offline Goran

  • LV2 Member (Next: 40)
  • **
  • Posts: 32
  • Rating: +0/-0
    • View Profile
Reuse of code/classes?
« on: October 01, 2012, 05:52:41 am »
Hello,

I would like to re-use my Lua classes (e.g. of vector algebra, special diagrams, etc.) that I often need in my calculations.

What is the "state-of-the-art" here: can I store the code to some module/library/file/whatever?

What about load()/loadstring() functions? Can they be used for this porpose?

Thanks and best regards,
Goran

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Reuse of code/classes?
« Reply #1 on: October 01, 2012, 06:12:25 am »
Hi,

Sadly, you can't right now have some kind of external module you can load easily. There are some tricks though (if you're really interested we can help you, it's about loadstring() indeed)

but generally you can build multiple scripts and each will have its own library ?

Would you have a precise example of some reusable code you would like to have multiple times ?
« Last Edit: October 01, 2012, 06:12:36 am by adriweb »
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Reuse of code/classes?
« Reply #2 on: October 01, 2012, 07:06:32 am »
I assume that you want include 'libraries' that you make in multiple projects.
In that case, I made this: https://github.com/jimbauwens/ETK/tree/master/buildtools .
However it is important to know that this uses Luna, and not TI's SDK.

TI them self haven't created anything like this for their SDK.

Offline Goran

  • LV2 Member (Next: 40)
  • **
  • Posts: 32
  • Rating: +0/-0
    • View Profile
Re: Reuse of code/classes?
« Reply #3 on: October 01, 2012, 07:48:36 am »
Hello,

the main problem with copying/pasting a source code from a script to another script is its multiple existence;
the problem that bothers me here it's not that much of unnecessary extra storage occupation, but rather of the fact
that if I find a bug and fix it in a particular copy, I have to remember all the places and to do the same multiple times.
The same is true if I enhance the "common" code during work on some script.

I saw in the Lua API Reference Guide the warning regarding load()/loadstring() functions: I have understood it in a way
we cannot edit/debug such code in the software, we have to rely on it on "as-it-is" basis, but we can use it w/o problem (assumed it does not contain bugs).
Is this correct?

If we store the code to TI-Nspire variables as a string, can we save/load the variables themselves to a file on the handeld?
Actually, which file types except .tns can we save/load under v3.2?
(e.g. in the printed manual I saw a snapshot of Document Toolbox with a .csv file among handheld files)

(also, I saw some 8* and 9* and v2* file compatibility list for CX, but is this some backward compatibility that CX only reads in?)

Best regards,
Goran

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Reuse of code/classes?
« Reply #4 on: October 01, 2012, 08:00:15 am »
Let my explain what my my script in the above post does.

It's a build tool that will use luna to create a tns file from a .lua file.
If the the lua file has a '--include file' comment, it will fetch the content of file, and place it instead of that comment.
So, you could have multiple programs like this:
Code: [Select]
--include 'libs/lib.lua'
--include 'libs/networking.lua'

function on.paint(gc)
.....

That way you don't need to  keep a copy of the library in every source file.

There is also the way you mentioned, storing your lib code in an external library document file, and using var.recall and loadstring to load it.
However, this is a complex and slow method. (Building the external library file, making sure you refresh the libs all the time, etc)

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Re: Reuse of code/classes?
« Reply #5 on: October 01, 2012, 11:09:16 am »
Oh why didn't I know of its existence before :o this is extremely useful since I'm working on a pretty large project :)
« Last Edit: October 01, 2012, 11:09:23 am by ElementCoder »

Some people need a high five in the face... with a chair.
~EC

Offline Lionel Debroux

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2135
  • Rating: +290/-45
    • View Profile
    • TI-Chess Team
Re: Reuse of code/classes?
« Reply #6 on: October 01, 2012, 01:04:57 pm »
Quote
(also, I saw some 8* and 9* and v2* file compatibility list for CX, but is this some backward compatibility that CX only reads in?)
The Nspire series is completely incompatible with both the TI-Z80 and the TI-68k series. The CX non-CAS does not have an interchangeable keyboard and cannot emulate a strange 84+SE (TI's 84+SE emulator for the Nspire Clickpad / Touchpad being both incomplete and slow).
Member of the TI-Chess Team.
Co-maintainer of GCC4TI (GCC4TI online documentation), TILP and TIEmu.
Co-admin of TI-Planet.

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Reuse of code/classes?
« Reply #7 on: October 01, 2012, 01:55:10 pm »
The Nspire series is completely incompatible with both the TI-Z80 and the TI-68k series.
However, you can "easily" adapt a 68k Basic application as the language isn't very different (but don't count on any graphic-related functions in the Nspire's version. For that you should look at Lua :-) )
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation