File-like option where the "file contents" comes from a string?

Andy Valencia dont at spam.me
Wed Nov 27 14:37:03 UTC 2024


On Wednesday, 27 November 2024 at 02:43:21 UTC, Ali Çehreli wrote:
> On 11/26/24 5:12 PM, Andy Valencia wrote:
>> Again and again for testing I run into how nice it would be to 
>> have an open "File" which has its contents set by the unit 
>> test code:
>> 
>>> auto f = StringFile("my test data...", "r");
> An earlier post by H. S. Teoh may be helpful:
> https://forum.dlang.org/post/mailman.10192.1685482943.31357.digitalmars-d@puremagic.com

Thank you.  It's somewhat bemusing to, in essence, wrap the 
entirety of an implementation around a #define just to slot in an 
alternative to File.  With my OO programming goggles on, I can't 
help but notice that if Phobos had made File a class--or an 
interface--then it would be possible through simple type 
compatibility to have a FileFile instance which would satisfy the 
File API using the filesystem, and as no special case have a 
StringFile which did the same from the body of a string.

But I used your suggested approach, and it does indeed work 
nicely.  Thank you again.

Andy



More information about the Digitalmars-d-learn mailing list