04a42335ad6f — Leonard Ritter tip an hour ago
* property: support `Printer` protocol
3 files changed, 39 insertions(+), 13 deletions(-)

M lib/scopes/compiler/Printer.sc
M lib/scopes/property.sc
M testing/test_property.sc
M lib/scopes/compiler/Printer.sc +1 -1
@@ 591,7 591,7 @@ type+ bool
 type+ real
     inline __printer (self ss)
         s := alloca-array char 32
-        c := snprintf s 32 "%g" self
+        c := snprintf s 32 "%f" self
         sz := clamp c 0 31
         ss (Styled.Number (/data s sz))
 

          
M lib/scopes/property.sc +3 -0
@@ 46,6 46,9 @@ inline property (getter setter)
             inline __toref (self)
                 @ (get self)
 
+            inline __printer (self print)
+                print (get self)
+
             inline __methodcall (name self ...)
                 name (get self) ...
 

          
M testing/test_property.sc +35 -12
@@ 15,17 15,40 @@ do
                 inline "setter" (self value)
                     self.float = value as f32
 
-    local iof = (IntOrFloat 3.0)
+    do
+        local iof = (IntOrFloat 3.0)
+
+        print iof.float
+        print iof.int
+        test (iof.float == 3.0)
+        test (iof.int == 3)
+        iof.int = 11
+        test (iof.float == 11.0)
+        test (iof.int == 11)
+        iof.int += 1
+        test (iof.int == 12)
+        test
+            (repr iof.int) == (repr 12)
+        print (repr iof.int)
+        print (tostring iof.int)
 
-    print iof.float
-    print iof.int
-    test (iof.float == 3.0)
-    test (iof.int == 3)
-    iof.int = 11
-    test (iof.float == 11.0)
-    test (iof.int == 11)
-    iof.int += 1
-    test (iof.int == 12)
-    test
-        (repr iof.int) == (repr 12)
+    do
+        # once more with new implementation
+        using import compiler.Printer
+        local iof = (IntOrFloat 3.0)
+
+        print iof.float
+        print iof.int
+        test (iof.float == 3.0)
+        test (iof.int == 3)
+        iof.int = 11
+        test (iof.float == 11.0)
+        test (iof.int == 11)
+        iof.int += 1
+        test (iof.int == 12)
+        test
+            (repr iof.int) == (repr 12)
+        print (repr iof.int)
+        print (tostring iof.int)
+
 ;