[futurebasic] Re: [FB] Good globals

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : December 2011 : Group Archive : Group : All Groups

From: Robert Purves <listrp@...>
Date: Fri, 2 Dec 2011 20:35:58 +1300
Max Taylor wrote:

> Off subject wish list.
> Floating point constants.

Not off topic. The required declaration in FB5 is a little verbose.

BeginCFunction
  const double kMyDoubleConstant = 1.23456;
EndC
BeginCDeclaration
  extern const double kMyDoubleConstant;
EndC
system double kMyDoubleConstant


You can now use kMyDoubleConstant anywhere an f.p. literal could occur.
The compiler won't let you change the value.
    _2_test.c:42:21: fatal error: read-only variable is not assignable
       kMyDoubleConstant = (double)0; 
       ~~~~~~~~~~~~~~~~~ ^

Unlike ordinary global variables, constants rarely complicate your code by posing subtle logic puzzles. When you see one, you *know* it's immutable. 

A simpler-seeming definition makes gMyDoubleConstant 'read-only' by agreement, enforced by programmer discipline but not by the compiler.
   dim as double gMyDoubleConstant : gMyDoubleConstant = 1.23456

Robert P.