1. sprintf() query

First, I noticed that the printf() docs do not state the third argument is optional (since 4.1)

Given that printf(1,"hi") is now legal, is there any case for sprintf("hi") to be legal as well - offhand I cannot think of any, which is why I am asking.

Pete

new topic     » topic index » view message » categorize

2. Re: sprintf() query

petelomax said...

First, I noticed that the printf() docs do not state the third argument is optional (since 4.1)

just slow in rewriting the docs!

petelomax said...

Given that printf(1,"hi") is now legal, is there any case for sprintf("hi") to be legal as well - offhand I cannot think of any, which is why I am asking.

Pete

Everything should work and look the same. A language is simple only if there are no surprises. So sprintf("hi") must work the same as printf. The slow doc writers job is easier; the language designer's job is also easier because there is a pattern to follow.

I still have questions like:

  • should the first argument be "output direction", when it is almost always "1" ?
  • should an output routine be able to output utf32 content to a utf8 terminal?
  • can the Phix string allow for a universal output routine that shows text and numbers "as expected"?
  • is it possible to create a new set of output routines that breaks from the DOS legacy?

_tom

new topic     » goto parent     » topic index » view message » categorize

3. Re: sprintf() query

_tom said...

should the first argument be "output direction", when it is almost always "1" ?

Erm, "output device". Making printf("hi\n") work as printf(1,"hi\n") (and leaving printf(fn, "hi\n") undamaged) is a perfectly good and none-too-difficult suggestion.

_tom said...

should an output routine be able to output utf32 content to a utf8 terminal?

Unquestionably. The big roadblock on that, I think, is automatically configuring/detecting a Windows console to be utf8 (chcp 65001 or chcp 28591) and with a truetype font, rather than with printf() itself.

_tom said...

can the Phix string allow for a universal output routine that shows text and numbers "as expected"?

Not quite sure what you mean. Examples?

_tom said...

is it possible to create a new set of output routines that breaks from the DOS legacy?

Maybe. It will help no-one to deliberately break legacy code. As above, better detection is probably the key, and/or you can always create eg printfN().

Pete

new topic     » goto parent     » topic index » view message » categorize

4. Re: sprintf() query

(*) everything is a number
(*) numbers can be used to represent text characters

I can identify three styles of output. (conventional languages only offer the 'data-type' style.)

(1) programmer output "the programmer chooses the look for the output."

(2) data output "the data chooses the look for the output"

(3) data-type output "the data-type chooses the look for the output."

Much more flexible than any conventional language.

-- programmer output 
 
object x  
	x = 1234 
	print(1, x) 	-- you chose 'print' for number output 
	--> 1234 
	 
	x = "hello" 
	puts(1, x)     -- you chose 'puts' for text output 
	--> hello 
-- data output 
-- warning: can not tell if a small integer is a character or a number 
 
object x 
	x = 1234 
	pp(x)		-- data seems to be numeric; output is numbers 
	--> 1234 
	 
	x = "hello" 
	pp( x )      -- data seems to be text; output is text 
	--> "hello" 
 
include std/pretty.e 
	pretty_print( x )	-- both numbers and text shown simultaneously 
	--> {104'h',101'e',108'l',108'l',111'o'} 
-- data-type output 
-- must invent a new procedure 'write' 
 
object x = 1234      -- objects are numeric 
string str = "hello" -- strings are text 
 
	write( x )		-- numeric data-type; numeric output 
	--> 1234  
 
	write( str )	-- string data-type; string output 
	--> hello 

Details still to be worked out.

_tom

new topic     » goto parent     » topic index » view message » categorize

5. Re: sprintf() query

It seems like you are describing object orientation.

-- (this is nothing new or exciting, just a lead-in for the sentence that follows) 
enum NUMBER,STRING -- classes 
 
object x = {NUMBER,1234}         -- or new_number(1234) 
object str = {STRING,"hello"}    -- or new_string("hello") 
 
enum CLASS,DATA  -- internal class structure 
 
constant formats = {"%d","%s"} 
 
procedure write(object x) 
  integer class = x[CLASS] 
  string fmt = formats[class] 
  printf(1,fmt,{x[DATA]}) 
end procedure 
write(x) 
 
-- or maybe something more like 
procedure call_method(string name, object x) 
  integer class = x[CLASS] 
  integer rdx = find(name,rtn_names[class]) 
  call_proc(rids[class][rdx],{x[DATA]}) 
end procedure 
call_method("write",x) 

What you cannot do is rely on the declaration type - and that is just as true for a full-blown object orientated system as it is OE/Phix - but on the actual object creation method - the value must get tagged somehow.

To clarify that, in an OOP system:

abstract class b {} 
concrete class c extends b {} 
concrete class d also extends b {} 
 
b myb; 

then obviously the declaration of myb may limit your choices but does not determine what routines get invoked or what it is, but rather that all depends on whether you put a c or d in it.

Pete

new topic     » goto parent     » topic index » view message » categorize

6. Re: sprintf() query

petelomax said...

It seems like you are describing object orientation.

That was accidental! I am just trying to describe what I see happening right now.

? procedure

What I just tested on Phix:

object x = 1234 
? x --> 1234 
 
puts(1, x ) 
--> "the pi character" 
--( virtual win7) 
 
string str = "hello" 
? str 
-- "hello" 

Looks like the string data-type is tagged as a text; as a result ? produces text. But for other data-types (atom,sequence) it produces numbers.

There is automagic here based on the data-type.

petes pretty printing

the pp procedure "looks" at values and adapts to show text or numbers.

(pp is a wonderful invention that must be added to OE)

Euphoria output

print and puts, user selects the nature of the output

our traditional thinking

"write"

just exploring the possibility of extending the properies of '?' that exist right now

_tom

new topic     » goto parent     » topic index » view message » categorize

7. Re: sprintf() query

in a Phix string, how do I output "hello" as numbers?

new topic     » goto parent     » topic index » view message » categorize

8. Re: sprintf() query

_tom said...

in a Phix string, how do I output "hello" as numbers?

pp("hello",{pp_StrFmt,1})  -- shows {104,101,108,108,111} 

In Phix, you can also do this:

?("hello"&-1)[1..$-1] 

The &-1 auto-expands the string to a dword-sequence (and the () stop it from trying to subscript the -1).

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu