tomhrr / dale

Lisp-flavoured C

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

add overloaded print functions for debugging

porky11 opened this issue · comments

I'm currently writing this, it already seems nice
Here some example usage
https://github.com/porky11/dale/blob/print/modules/print.dt#L88
it's similar to c++ stream operator<< but looks better, and also returns the printed value like lisp (this will be done as macro, so this won't become inefficient)

commented

Would also be cool if there was a type-safe format macro like the one in Rust for example. Or how printf works in OCaml.

Yes, rusts print! and println! macros also look good for printing. I think, selecting, how something is printed, should be dispatched at compiletime, not at runtime like in c.
I don't know about printf in OCaml

commented

If I remember correctly. Printf in ocaml is much like printf or like rusts print macros. Except that I think it's built in to the compiler to type check at compile time (and probably more optimized than C printf). In dale we should be able to implement this as a library instead of in the compiler.

In rust this is implemented using the traits for normal printing and debug printing. In dale implementing a concept for printing would be similar, but since concepts cannot be implemented automatically (yet?), so something like my version is more usable, maybe with some compile time checking for existance.
Why should a format string be used? Isn't (print "Hi, my name is " name " and I'm " years " years old") easier to read and write than (print "Hi, my name is ~a and I'm ~a years old" name years). In the last case, it's easier to see all arguments, that may be mutable, but they may also be constants.

commented

Format strings are nice sometimes. For example if the variable names are very long it is easier to see the format of the string to get a sense of what it will look like. And then you just gotta make sure the order is right etc. But yeah. I think both should be offered.

something like (print "Hi, my name is {name} and I'm {years} years old") would also be possible (I think, ren'py uses something like this). You even could have reader macros which expand #"String {Var}" to (string-append "String " var).

commented

Yeah I'm all for that kind of feature. But still it doesn't solve the issue that the variable names that you are interpolating might be really long. So you might wanna have the option of using safe format strings.