Wanted: Format character for source code literal

Q. Schroll qs.il.paperinik at gmail.com
Wed May 5 20:38:54 UTC 2021


On Wednesday, 5 May 2021 at 19:53:10 UTC, Q. Schroll wrote:
> ## Implementation
>
> Now, let's talk about the implementation. It's far easier to 
> talk about that in terms of a function. Let's call it `unMixin` 
> because the goal is that `mixin(unMixin(obj))` results in `obj` 
> or a copy of `obj`. On the other hand, we cannot expect 
> `unMixin(mixin(str))` to return `str` because `str` could 
> contain unnecessary information and even if it doesn't, it can 
> contain context-dependent information that `unMixin` cannot 
> generally retrieve.
>
> Simplest example: If `unMixin(1)` returns `"1"`, we're good for 
> `1`. If it returns `"cast(int) 1"`, we're also good.

I've done some experiments and the results are mixed.

The easiest by far is `typeof(null)`. For [scalar 
types](https://dlang.org/library/std/traits/is_scalar_type.html) 
and strings, the aforementioned `%(%s%)` can be used.

Pointers and slices aren't too hard either.

For structs without a constructor, `unMixin` is actually easy; if 
it has a constructor, the object cannot be described by a 
constructor call since who knows what the constructor does and 
maybe there isn't even a simple constructor call that will result 
in the given object. It can be done, but it's ugly and hacky.

Because unions can have sub-structs and stuff, I gave up on them.

I have not too much experience with D's classes, but from my 
estimation, it cannot be done. It looks like you need `typeid` at 
compile-time (at CTFE to be precise) which isn't available.

My take on it so far: 
https://run.dlang.io/gist/c98ef765cb8921595d5e41fc11c89ca7?args=-unittest%20-main


More information about the Digitalmars-d mailing list