Const

Jimmy

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,021
Is there a way to calculate constants at compiletime?

Code:
Import mojo2

Const LOG10:Float = 2.3025850929
' Const LOG10:Float = Log(10)

Function Main ()
    New Game
    Return 0
End   

Class Game Extends App
    
    Method OnCreate ()
        Print digitCount(1234)
        Return 0
    End
 
End

Function Log10:Float(num:Float)
    Return Log(num) / LOG10
End Function

Function digitCount:Float(num:Float)
  If num = 0 Then Return 1
  Return Floor(Log10(Abs(num))) + 1
End Function
 

Jimmy

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,021
There's no way to use the Final keyword on variables to make programmatically created constants possible
 

MikeHart

Administrator
Joined
Jun 19, 2017
Messages
3,208
The Final keyword defines a class as not being extendable and it's methods not being overwritable.
 

Jimmy

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,021
So not as in JAVA where you may declare that a variable cannot be modified. To bad.
There's no # or macro trick you can use either I guess.

No biggie I'll go with comments then.
 
Top Bottom