hi miller & devs ...
is this: |float 4( | |makefilename CR%sSH| |
intended to crash?
i'd somehow prefer to have a proper error message or a type conversion from float 4 to symbol 4 instead of a segfault ...
cheers ... tim
On Sat, 8 Jan 2005, Tim Blechmann wrote:
|float 4( | |makefilename CR%sSH| intended to crash? i'd somehow prefer to have a proper error message or a type conversion from float 4 to symbol 4 instead of a segfault ...
Hi. Let's use precedent from another language. For example, let's first look at the sprintf from Ruby:
"%s %s %s" % [3.14,"test",:test] gives "3.14 test test" "%s %s %s" % [3.14,"test"] gives error (list too short) "%f %f" % [3.14,:test] gives error (can't conv to float)
In the first case, 3.14 is a Float, so it gets converted to a String before applying %s; also :test is a Symbol, so it also gets converted to a String before applying %s.
However not all conversions are applicable, and it is important to match list lengths with format strings (each %-code of a String requires one or two or zero arguments, according to the same rules as in C language).
Trying the same with Python, I get similar results. The differences are: there is no Symbol datatype, only String; I had to replace [] by () because Python has two kinds of lists and it wants ()-lists there; and the Floats have a different default formatting in converting to String, apparently %.6f instead of just %f.
Plus I'd rather have [makefilename] be named differently, because it clearly gets used for much more than just filenames. maybe [formatsymbol]. I'd suggest [sprintf] if not for nameclashes.
_____________________________________________________________________ Mathieu Bouchard -=- Montréal QC Canada -=- http://artengine.ca/matju
Plus I'd rather have [makefilename] be named differently, because it clearly gets used for much more than just filenames. maybe [formatsymbol]. I'd suggest [sprintf] if not for nameclashes.
sprintf is a wonderful cyclone object ... with type-checking ...
cheers ... tim
On Sat, 8 Jan 2005, Tim Blechmann wrote:
Plus I'd rather have [makefilename] be named differently, because it clearly gets used for much more than just filenames. maybe [formatsymbol]. I'd suggest [sprintf] if not for nameclashes.
sprintf is a wonderful cyclone object ... with type-checking ...
And so is [rubysprintf]. well, the object (class) itself is not wonderful by itself, you have to attribute that to Ruby itself. Here's the source code for [rubysprintf]:
class Sprintf < FObject def initialize(format) _1_symbol(format) end def _0_list(*a) a.each {|x| x=x.to_s if Symbol===x } send_out 0, :symbol, (sprintf @format, *a).intern end alias _0_float _0_list alias _0_symbol _0_list def _1_symbol(format) @format = format.to_s end install "rubysprintf", 2, 1 end
that's it. and it does full type-checking too. Of course, the same can be done with pyext, and maybe even in less lines (the Ruby external isn't particularly polished... I'll be working on it soon enough)
_____________________________________________________________________ Mathieu Bouchard -=- Montréal QC Canada -=- http://artengine.ca/matju
On Sun, 9 Jan 2005, Mathieu Bouchard wrote:
And so is [rubysprintf]. well, the object (class) itself is not wonderful by itself, you have to attribute that to Ruby itself. Here's the source code for [rubysprintf]:
This was a nice blunder of mine, which could have been 95-99% unnoticed if I wasn't bringing attention to it right now. I almost did not notice it.
Ok, here's a really nice (NOT!) methodology for debugging code:
0. write some part of the source code, in a few minutes, while doing something else, and never look back, because it does just what you happen to ask of it.
1. post some small example of your source code on a much-read int'l mailing-list, and bring particular attention to that snippet.
2. read the mail when it comes back through the mailing-list software and stare at it stupidly.
3. find a really stupid bug in the really small example.
So, for the few who can code in Ruby (or, particularly, are currently learning to) you can, from here, just skip back to the previous mail and try to find the bug, as a quiz.
. . .
Answer: Assigning to the block-variable x has no effect, as it is just a copy of a slot of the array, instead of being the slot itself (contrast to Perl's almost-equivalent "foreach my $x (@$a) { ... }").
So, to convert all symbols of a list to strings, and not touch the floats, one could change that line like this:
a.each {|x| x=x.to_s if Symbol===x }
a.map! {|x| case x when Symbol; x.to_s ## cast to string... else x ## else keep it as is end } ## map! is like each but replaces each element by the return value
or (closer in style to the former):
a.map! {|x| if Symbol===x then x.to_s else x end }
but then, my actual replacement looks like this:
... That's it, I just deleted the line. The reason is that, in the handling of "%s", Ruby already applies .to_s on whichever it gets, so both Symbols and Floats are accepted in %s.
However, in several other cases, the .to_s is not automatic (and some of my externals have lines of code like the above)
I thought about mapping Pd symbols directly to Ruby strings instead, which would be slightly less trouble, but it's sort of too late now... Anyway I reserve Ruby's string type to match to the upcoming Pd strings (whatever they will be).
_____________________________________________________________________ Mathieu Bouchard -=- Montréal QC Canada -=- http://artengine.ca/matju
I thought about mapping Pd symbols directly to Ruby strings instead, which would be slightly less trouble, but it's sort of too late now... Anyway I reserve Ruby's string type to match to the upcoming Pd strings (whatever they will be).
Hmmm, what a coincidence. Luckily, it hasn't been too late for pyext. Thus a Python Symbol type will show up in tomorrow's cvs, speeding up things.
best greetings, Thomas