Thoughts about "Compile-time types" talk

H. S. Teoh hsteoh at quickfur.ath.cx
Wed May 15 19:09:27 UTC 2019


On Wed, May 15, 2019 at 06:31:57PM +0000, NaN via Digitalmars-d wrote:
> On Tuesday, 14 May 2019 at 17:44:17 UTC, H. S. Teoh wrote:
> > On Mon, May 13, 2019 at 08:35:39AM +0000, Martin Tschierschke via
> > Digitalmars-d wrote:
> > > On Friday, 10 May 2019 at 00:33:04 UTC, H. S. Teoh wrote: [...]
> > > > I haven't fully thought through this yet, but the basic concept
> > > > is that there should be *no need* to distinguish between
> > > > compile-time and runtime in the general case, barring a small
> > > > number of situations where a decision has to be made.
> > > [...]
> > > I was thinking and working in the same direction, when using regEx
> > > you can use the runtime (RT) or the compile time (CT) version, but
> > > why not let the compiler make the decisions?
> > 
> > It would be great if the compiler could make all of the decisions,
> > but I think at some point, some level of control would be nice or
> > even necessary.
> 
> If you envisage a regular if statement being able to be both CT/RT
> depending on whether the value inside its brackets is known at CT or
> not what do you do about whether it introduces a new scope or not?
> Static if does not, but regular if does, if you want them unified
> something has to give.
[...]

I think the simplest solution would be for regular if to always
introduce a new scope, and static if behaves as before. Due to their
different semantics on the surrounding code, blindly unifying them would
probably be a bad idea.

But taking a step back, there are really two (perhaps more) usages of
static if: (1) to statically select a branch of code based on a known CT
value, or (2) to statically inject declarations into the surrounding
code based on some CT condition.

Case (1) is relatively straightforward to unify with regular if.

Case (2) would seem to be solely in the domain of static if, and I don't
see much point in trying to unify it with regular if, even if such were
possible.  For example, static if can appear outside a function body,
whereas regular if can't. Static if can therefore be used to choose a
different set of declarations at the module level. If we were to
hypothetically unify that with regular if, that would mean that we have
to somehow support switching between two or more sets of
likely-conflicting declarations at *runtime*.  I don't see any way of
making that happen without creating a huge mess, unclear semantics, and
hard-to-understand code -- not to mention the result is unlikely to be
very useful.

So I'd say static if in the sense of (2) should remain as-is, since it
serves a unique purpose that isn't subsumed by regular if.  Static if in
the sense of (1), however, can probably be unified with regular if.


T

-- 
Genius may have its limitations, but stupidity is not thus handicapped. -- Elbert Hubbard


More information about the Digitalmars-d mailing list