SuperBASIC function for type validation

Anything QL Software or Programming Related.
User avatar
pjw
Super Gold Card
Posts: 686
Joined: Fri Jul 11, 2014 8:44 am
Location: Norway
Contact:

Re: SuperBASIC function for type validation

Postby pjw » Fri Jul 10, 2020 5:30 pm

I cant remember whether or not WHEN ERRor is part of SuperBASIC. Q-Liberator has its own variant, as does Turbo.

Personally I prefer my own VALID% toolkit function (naturally):

ok% = VALID%(expected_type%, <variable or expression>)

It uses the standard Qdos calls to try to fetch the variable/expression on the
stack as the type you specify (int, long, float), and returns its success or not
in ok%. The advantage of this method is that its interpretation will be 100%
consistent with the S*BASIC interpreter's idea of a variable's validity.

For example x% = '1e700' is a valid integer! Namely 1. However, x = '1e700' is
not a valid float! It may not be easy to cater for such anomalies in a home-grown
validy-checking program. At least not without WHEN ERRor. Much better to let
S*BASIC do the hard work!

PRINT VALID%(3, '1e700') will return ok
PRINT VALID%(2, '1e700') will return not ok (2 being the code for float)

VALID%, including it source code (so you could, for example, add it to your own
bespoke toolkit) can be found at Knoware.no


Per
Be wary of large West-Coast corporations bearing gifts!
- Ancient Trojan proverb
User avatar
dex
Trump Card
Posts: 233
Joined: Thu Dec 23, 2010 1:40 pm

Re: SuperBASIC function for type validation

Postby dex » Fri Jul 10, 2020 5:44 pm

I cant remember whether or not WHEN ERRor is part of SuperBASIC

QL ROM (post JM)
This SuperBASIC structure does not work very reliably on any QL versions other than Minerva v1.77 (or later), SMS or the THOR XVI: although Toolkit II improves the reliability...
https://superbasic-manual.readthedocs.io/en/latest/W/when--error.html


RWAP
RWAP Master
Posts: 2712
Joined: Sun Nov 28, 2010 4:51 pm
Location: Stone, United Kingdom
Contact:

Re: SuperBASIC function for type validation

Postby RWAP » Sat Jul 11, 2020 11:20 pm

Good to see people are using and quoting from my SuperBASIC / SBASIC Reference Manual :D



Who is online

Users browsing this forum: No registered users and 4 guests