Question: Is there a builtin way to add a Units type to a variable (i.e. typing a variable) without first adding a value or another undefined variable?

This should be pretty simple. 

    local var::Unit(kg);     #generates a warning
    var := 1;
    var;                     #-> 1 kg... alas this doesn't work (it's just 1)

In other words, I want to declare a variable with an unknown quantity and known `Unit` at the outset similar to how things are done in C-style languages, and later set the numeric value. The goal is to make it easier to manipulate functions that deal with lots of different physics unit types without having to write them each individual line. For example on lines 10-13 I manually include the units when it would be more convenient if they were already tied to the variable they're associated with.

Solving a simple physics problem to find the radius of a planet with the universal gravitational constant & known density:

I know types exists in Maple, i.e. 

    evalfr := proc(expr, iexact::integer := 0) 
      if   0 < iexact   then  return convert(evalf(expr), rational, iexact); 
      elif iexact = -1  then  convert(evalf(expr), rational, exact); 
      else                    return convert(evalf(expr), rational); 
    end if; end proc

This just being a wrapper with `iexact` typed as an `integer` setting up a set of conditionals around `evalf` to rationalize an expression when using `ScientificConstants`.

I've considered using anonymous functions to emulate this behavior with setters and getters, but that seems like overkill for something that is probably builtin. For the time being I have configured my worksheet so it references secondary variables, 

    var := var2*Unit(kg)

This strikes me as somewhat inelegant. Also trying to use the same variable name in-line (i.e. `var := var*Unit(kg)`) gives `Error, recursive assignment`.

I am curious if there is a correct Maple way to do something like this. Or would I have to implement some sort of anonymous function to get the behavior I want? Appreciate any help!


Please Wait...