Up to Main Index                           Up to Journal for October, 2023

                   JOURNAL FOR SATURDAY 21ST OCTOBER, 2023
______________________________________________________________________________

SUBJECT: Boxing and unboxing variables
   DATE: Sat 21 Oct 20:09:18 BST 2023

No, the holiday season has not come early, this is about boxing and unboxing
variables and a new ‘any’ type :/

After releasing Mere v0.0.8 last weekend, I’ve been busy working on Mere’s
compile time checking. The hope is to be able to flag as many errors as
possible at compile time and remove many of the runtime checks. Doing so has
not been easy.

In order to make things easier, what I’ve actually been working on is adding a
new ‘any’ type. A variable of type any can have a value that is any of Mere’s
types. As you can imagine this by itself would lead to chaos.

In order to avoid chaos, the initial implementation will be very simple and
very restrictive: the ‘any’ type will only support manual conversions.

For example:


    x := 1      // x is an int
    a := any x  // conversion of int to any (boxing)
    y := int a  // conversion of any to int (unboxing)


Having an ‘any’ type creates an interesting problem with the type built-in.

What should the type built-in return for ‘a’ above? Should it be ‘any’ or
‘int’? What would we be most interested in? The fact that ‘a’ is of type ‘any’
or that ‘a’ contains an int? I think we are more interested in the fact that
‘a’ contains an int. For example consider this code:


    x:int y:int >< 1 2
    call add(any x, any y)    // auto-boxing would make this call nicer…

    add: func a:any b:any
      if type a != type b
        println "Cannot add a and b, different types."
      elif type a == "int"
        println int a + int b
      elif type a == "uint"
        println uint a + uint b
      elif type a == "float"
        println float a + float b
      elif type a == "string"
        println string a + string b
      else
        println "Cannot add a and b, not numbers or strings."
      fi
    endfunc


In the code above we are obviously more interested in the type ‘a’ and ‘b’
contain. But what if we want to know that ‘a’ is actually defined as type any?
For example when we are debugging. One possible solution would be to use the
literal built-in:


    >cat any.mr
    x := 1
    a := any x
    println a
    println literal a
    >mere any.mr
    1
    any(1)
    >


One area where this will have significant impact is in the use of maps. All
map elements are of type any already. Now the values will have to be boxed and
unboxed:


    m:[int]
    m[1] = any "one"        // boxing
    s:string = string m[1]  // unboxing


Obviously this is an example where auto-boxing and auto-unboxing would be
desired. With auto-boxing and auto-unboxing the code would look just as it
would now:


    m:[int]
    m[1] = "one"      // auto-boxing
    s:string = m[1]   // auto-unboxing


That’s phase two. For now I’m just trying to get the ‘any’ type working in a
sane manner that also fits in nicely with the current Mere language :(

Only ten days left until Halloween!  ╓╥ôô╥╖  ;)

--
Diddymus


  Up to Main Index                           Up to Journal for October, 2023