Can't get operator to work.

35 views
Skip to first unread message

Vernon Brown

unread,
Oct 19, 2019, 7:06:33 PM10/19/19
to ParaSail Programming Language
I created a simple Integer formatting function that let's you right align an integer image in a string with a specified width.
It's defined as:
  func Format(Format_Str : Univ_String; Value : Univ_Integer) -> Univ_String

An example call to Format is:
  var y := "3d".Format(x)

The above works just fine, but seems a little "wordy".
So, in the spirit of the ` operator, I tried to replace Format with "+" using the declaration:
  op "+"(Format_Str : Univ_String; Value : Univ_Integer) -> Univ_String is Format;

But, I can't get this operator to work at all.

I tried:
  y := "3d"+x;
  y := "3d"."+"(x);
  y := "+"("3d",x);
  y := Univ_String::"+"("3d",x);
none of which worked.

I get errors similar to:
  Error: use of op "+" not resolved:
  Info:   "3d" + x
  Info: operand's type Univ_String<> does not match param type Univ_Integer<>
  Info: operand "3d" does not match Left : Univ_Integer
  Error: Cannot resolve type:

What am I doing wrong?

I attached a working example with the non-working lines commented out.
Format.psl

Tucker Taft

unread,
Oct 21, 2019, 5:06:26 PM10/21/19
to ParaSail Programming Language
Operator visibility is special.  Operators are only visible if they are defined where a type is defined and they have a parameter or result of the type.  So you would have to define a new type, e.g. a Format_String, and then you could define an operator that operated on it.  But even then you would still have a challenge, since you want a string literal like "3d" to be interpreted as a Format_String when "appropriate" and the ParaSail parser has a rough time doing that.

I have attached a version of Format.psl that works, but I had to help it by declaring a local Format_String "F" initialized from "3d" which doesn't really seem to achieve the conciseness you were hoping for.

What you might do is define a function with a shorter name, such as simply "f", as a rename of "Format" and then generally write:

      f("3d", x)
or
     "3d".f(x)

which is pretty economical in terms of characters.

Take care,
-Tuck

--
You received this message because you are subscribed to the Google Groups "ParaSail Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to parasail-programming...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/parasail-programming-language/dd6e28c8-1d7f-4aa0-ac8f-be0d54570825%40googlegroups.com.
Format.psl
Reply all
Reply to author
Forward
0 new messages