www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Custom type / Typle type formatted reader

reply Rekel <paultjeadriaanse gmail.com> writes:
It's more of a multi-part question,
I've been trying to read tuples using 'slurp', though later 
realised one of the types was an enum, I'm guessing that's what 
was the problem, which lead me down a whole rabbit hole.

- Can I directly read tuples using slurp? It doesnt seem to like 
slurp!TUPLE_ALIAS_HERE(etc

- Since slurp can read files into tuples, can I somehow tell 
slurp to name the fields?
(For example, I have `alias Instruction = Tuple!(OPERATION, 
"operation", int, "argument");`, but I'm not sure if it's 
possible to make slurp output one), or alternatively, can I 
define it's output tuple 'type'?

- If it's possible to make user types print in certain ways 
(https://wiki.dlang.org/Defining_custom_print_format_specifiers), 
is it possible to also read them back? Say using formattedRead or 
slurp or parse or even to? And if so, how does this work?

- Is it possible to read (formatted) data into enums, as for 
example I would like? I wouldnt be surprised if this question 
stands loose from the previous one, as it's not entirely the same 
situation.
I have an enum;
enum OPERATION {NOP, // Would like to read 'nop' hereto
		ACCUMULATE, // 'acc' likewise
		JUMP // and 'jmp'
		}
Dec 27 2020
parent reply Rekel <paultjeadriaanse gmail.com> writes:
🤔I'm either asking a stupid question, asking it in a wrong way, 
or asking an important question. Clueless nontheless. . .
Dec 31 2020
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 12/31/20 8:36 AM, Rekel wrote:
 =F0=9F=A4=94I'm either asking a stupid question, asking it in a wrong w=
ay, or=20
 asking an important question. Clueless nontheless. . .
Your post was interesting to me but trying to duplicate your situation=20 seemed difficult. Can you describe it with a piece of code? If the code shows what the=20 current undesired output is and what you want instead, perhaps we can=20 find a solution. Ali
Dec 31 2020
parent reply Rekel <paultjeadriaanse gmail.com> writes:
On Thursday, 31 December 2020 at 18:19:54 UTC, Ali Çehreli wrote:
 Can you describe it with a piece of code? If the code shows 
 what the current undesired output is and what you want instead, 
 perhaps we can find a solution.
Regarding the enum part; While trying to do this I noticed one cannot use either slurp or formattedRead to read enums, while it is possible to use formattedWrite and write to write them. Strangely enough this was also mentioned in old issue '18051' (https://forum.dlang.org/thread/mailman.735.1512835762.9493.digitalmars-d bugs puremagic.com) which should have been resolved. Example code; ``` import std.stdio; import std.file : slurp; import std.format; import std.typecons; enum Thing { A, B } void main() { File file = File("temp.txt", "r"); foreach (line; file.byLine) { int integer; Thing thing; formattedRead!"%s %s"(line, integer, thing); // crashes with thing as Thing, works with thing as char. formattedWrite(stdout.lockingTextWriter, "%s %s\n", integer, thing); } } ```
Dec 31
parent Rekel <paultjeadriaanse gmail.com> writes:
And regarding the rest,

I'm curious about reading back user types, without defining a new 
seperate method for it, such as structs, unions and tuples, 
(although I'm unsure to what extent tuples are really a 
user-defined-type).

The other thing just resolved itself;
I was wondering, as slurp kind of already reads tuples from 
files, whether or not it's possible to tell it what tuple you 
want it to read, instead of redefining a possibly previously 
aliased tuple.
It seems now I found a way to do just that . . . I honestly don't 
know why this went wrong before, must have made a mistake 
somewhere. This now works:

	alias Entry = Tuple!(int, "a", char, "b");
	Entry[] results = slurp!Entry("temp.txt", "%s %s");
	results.writeln;

Im actually kind of surprised it does, as nothing in the 
documentation seems to hint to me this makes sense, neither did I 
find any example like it elsewhere.
Dec 31