Two standard libraries?
renoX
renosky at free.fr
Sun Jul 29 07:56:27 PDT 2007
Chris Nicholson-Sauls a écrit :
> renoX wrote:
>> Steve Teale a écrit :
>>> Sean Kelly Wrote:
>>>
>>>> Steve Teale wrote:
>>>>> It seemes to me that given Walter's definition of the language - a
>>>>> system programming language - that Phobos is closer to the mark.
>>>>> If users want a more object oriented standard library, that's all
>>>>> well and good, but it should be a shoe-in, then if you want to use
>>>>> the OO stuff you can, but code that's been written to work with
>>>>> Phobos should work unmodified with other libraries. (Note the
>>>>> recent discussion on C++ security).
>>>> While one might argue that it is easier to wrap a strictly
>>>> procedural library with an OO layer than vice-versa, I think the
>>>> ease with which any library may be encapsulated in a wrapper is more
>>>> dependent on its design (the assumptions it makes, how features are
>>>> exposed, etc) than on whether the interface uses functions or
>>>> objects. That said, I don't personally consider Tango to be an
>>>> object-oriented library because it does not require the user to
>>>> define his own objects in order to use it.
>>>>
>>>>
>>>> Sean
>>>
>>> Sean, I take your point, and maybe should not have used the term
>>> "OO", but my idea of progress is:
>>>
>>> printf("X is: %s\n", toStringz(x))
>>> cout << "X is: " << x << endl;
>>> Stdout("X is: ")(x).newline;
>>> writefln("X is: %s", x);
>>
>> I agree with you that cout or Stdout strike me as particularly ugly
>> and that writefln is a (small) progress over printf.
>>
>> But when I see Ruby's way: puts("X is ${X}\n"); I can't help but
>> feeling that even writefln is not enough..
>>
>> renoX
>>
>
> Technically the Ruby sample would usually just be:
> puts "X is #{X}"
Oops, a long time since I've made Ruby thanks for the various
explanation of how it works in Ruby.
> Which is essentially shorthand for:
> $stdout.method(:puts).call('X is ' + X.method(:to_s).call());
>
> And if you need to format X any at all, you'd use something like this:
> puts 'X is %08x' % [X]
I don't like this syntax: the interesting part of #{X} is that the
variable name is located at the same point as its string representation
which reduce greatly the risk of mistake, when I've made my 'puts' like
function for D (which used template,posted a while ago) , I used %{X}
for normal embedding and allowed also %08x{X}.
I'm surprised that Ruby doesn't allow puts "X is #08x{X}"..
>
> Which is shorthand for:
> $stdout.method(:puts).call('X is %08x'.method(:"%").call(X));
>
> Which is a wrapper around:
> puts sprintf('X is %08x', X)
>
> Aka:
> $stdout.method(:puts).call(Kernel::sprintf('X is %08x', X));
>
> Uhm.... sorry, got a little carried away there. ;) But the main point
> was, in cases like this Ruby and D don't really compare -- what with the
> former being in a virtual environment and the latter a systems language
> meant to expose metal. For something like "X is #{X}" to work in D,
> wouldn't really require very much (symbol table, and runtime function to
> do the insertions) but the cost of such a thing becoming "mandatory"
> would be great.
It depends: when the string is known at compile-time (which happens
quite often), then there is absolutely no overhead over printf.
For runtime string, yes this syntax would have a significant overhead.
> I'd happily settle for a template or CTF that takes that string as
> parameter, and spits out an appropriate use of Stdout/writef.
Well I've posted one some time ago (improved format string), there
didn't seem to be a huge interest so I've stopped working on it (beside
with macros it should be better), but I can't help cringing with index
counting "{1} {2}" format string, barf..
renoX
>
> On a side note, call me when we can duplicate this Ruby in D:
>
> +-----[ Ruby ]-----------------------------------+
> |
> | def verb_arg_str (flags, which)
> | ['none', 'any', 'this'][(flags >> case which
> | when :dobj then VFSHIFT_DOBJ
> | when :iobj then VFSHIFT_IOBJ
> | end) & VFMASK_ARGS]
> | end
> |_________________________________________________
>
> Er... actually we can, sans the symbol type in favor of a bool:
>
> +-----[ D ]--------------------------------------+
> |
> | char[] verbArgStr (uint flags, bool dobj) {
> | return ["none"[], "any", "this"][
> | (flags >> (dobj ? VFSHIFT_DOBJ : VFSHIFT_IOBJ))
> | & VFMASK_ARGS
> | ];
> | }
> |_________________________________________________
>
> Huh, how about that.
>
> -- Chris Nicholson-Sauls
More information about the Digitalmars-d
mailing list