Up to Main Index                             Up to Journal for April, 2023

                      JOURNAL FOR FRIDAY 7TH APRIL, 2023
______________________________________________________________________________

SUBJECT: To type or not to type?
   DATE: Fri  7 Apr 19:42:42 BST 2023

No, this isn’t going to be another instance of me lamenting the sorry state of
the journal so far this year :( Instead, I want to talk about data types.

In the previous versions of Mere a variable’s type was defined by adding a
colon ‘:’ and type after the variable name:


    my_int:int = 1
    my_array:[]int = []int 1 2 3
    my_map:[int] = [int] 1 2, 2 1.1, 3 "a", 4 true


In the current version of Mere the types are inferred from assignment:


    my_int = 1
    my_array = []int 1 2 3
    my_map = [int] 1 2, 2 1.1, 3 "a", 4 true


The current version also makes it easy to change a variable’s type:


    x = 1               // x is an integer
    println x type x
    x = "a"             // x is now a string
    println x type x

    y = 3.5             // y is a float
    y = int y           // conversion of y to an integer
    println y type y


This results in:


    1 int
    a string
    3 int


In both cases Mere is strongly typed and there is no type coercion. You can’t
use a float in place of an integer. For example you can’t add an integer and a
float without a conversion:


    x = 3
    y = 5.7
    i = x + int y
    f = float x + y
    println i type i
    println f type f


This results in:


    8 int
    8.7 float


Now, I could make it so that variable types cannot be changed once set. This
would make Mere mostly[1] statically typed and enable compile time checks.

Or, I could leave it so variable types can be changed. This would make Mere
dynamically typed and need runtime checking.

If statically typed then Mere would still require some runtime checks. If
dynamically typed there could still be compile time checks.

For now Mere is what it is — strongly dynamically typed. This may change as I
develop the language more. Let me know your thoughts: diddymus@wolfmud.org

As an aside, due to a quirk in the current version, labels can be stored in
maps. This allows lookup tables of gotos and gosubs to be built:


    >cat coin.mr
    table = [int] 1 heads, 2 tails
    flips = 3
    flip:
      gosub table[rnd 2]
      if flips -->0; goto flip
    exit

    heads:
      println "heads!"
      return

    tails:
      println "tails!"
      return

    >mere coin.mr
    heads!
    tails!
    tails!


In other news…

Previously I’ve said that operators and built-in functions are still encoded
using their string representation. The plan was to eventually encode them as
integers for more efficient map lookups. That work has now been completed. As
a result counter.mr, my standard benchmark, is over twice as fast :)

For counter.mr the runtime has gone from 904ms down to 399ms (0.9s to 0.4s).
By comparison, fastest runtime for the old Mere was 3,530ms (3.5s). Either
way, quite a nice improvement ;)

It’s a long weekend for Easter in the UK. At the moment I’m pretty happy with
where Mere is at. I’m busy going through the Mere ICE documentation to bring
it up to date with the current development version. Mostly this involves
updating the examples and double checking for new functionality. Assuming all
goes well, I hope to update the online version this weekend so people can have
a play :) …and provide feedback ;)

--
Diddymus

  [1] The type of map elements are always defined by assignment, not by
      definition. Only the keys are defined:

        m = [int] 1 3.5, 2 "string"

      Here m[1] is a float with value 3.5 and m[2] is a string of value "a".


  Up to Main Index                             Up to Journal for April, 2023