[M3devel] saving indentation?

Jay K jay.krell at cornell.edu
Sat Sep 1 22:28:48 CEST 2012


"saving indentation"


So..let me try this yet one more time.
Modula-3 has some interesting and almost unique characteristics.
Optional safety and compiling to native code is fairly unique.


The syntax in general is limited. I have great difficulty getting comfortable with it.
I end up writing much less readable code.


I would really really like "to get easy cases out of the way early"
and "straightline" the code mostly -- I don't want to indent like crazy
and read through if after if after if.


Similarly, I want to initialize variables when I declare them, but not
indent like crazy.


Horizontal space is valuable.
Not leaving variables uninitialized is valuable.
Avoiding horrendous if/if/if/else if/else if/else "ladders" is often valuable.
It is hard to have all of these in Modula-3, but easy in C++ and C9x.



Here I have a string that I am checking if it 
starts with one of two prefixes and followed by an integer.


I would LIKE to write it like so:


        var-or-const name := M3ID.ToText(proc.name);
        var-or-const length := Text.Length(name);
        FOR i := FIRST(u.handler_name_prefixes) TO LAST(u.handler_name_prefixes) DO
            var-or-const prefix := u.handler_name_prefixes[i];
            var-or-const prefix_length := Text.Length(prefix);
            IF length <= prefix_length OR NOT TextUtils.StartsWith(name, prefix) THEN
                continue;
            END;
            var-or-const end = Text.Sub(name, prefix_length);
            FOR i := 0 TO Text.Length(end) - 1 DO
                IF NOT Text.GetChar(end, i) IN ASCII.Digits THEN
                    RETURN FALSE;
                END;
            END;
            RETURN TRUE;


        END;
        RETURN FALSE;




but Modula-3 doesn't have "continue", right, and var/with imply indentation,
So I have to write:
        WITH    name = M3ID.ToText(proc.name),
                length = Text.Length(name) DO
            FOR i := FIRST(u.handler_name_prefixes) TO LAST(u.handler_name_prefixes) DO
                WITH    prefix = u.handler_name_prefixes[i],
                        prefix_length = Text.Length(prefix) DO
                    IF length > prefix_length AND TextUtils.StartsWith(name, prefix) THEN
                        WITH end = Text.Sub(name, prefix_length) DO
                            FOR i := 0 TO Text.Length(end) - 1 DO
                                IF NOT Text.GetChar(end, i) IN ASCII.Digits THEN
                                    RETURN FALSE;
                                END;
                            END;
                            RETURN TRUE;
                        END;
                    END;
                END;
            END;
        END;
        RETURN FALSE;



Isn't the first much more readable?

Is there anything to do about this?

Am I crazy? Introducing variables "anywhere", without a need to open a block,
is considered one of the most conservative features of C++.
It is in C9x.
The downside of it is that you can't as easily look at the top of a function
and guestimate how much stack it uses.


Furthermore, I want "const" to mean "relatively const" -- do whatever
codegen is needed to initialize it, but then don't let me change thereafter.
Not "the compiler can evaluate it at compile time".


It is useful, it reveals code to be "psuedo functional".
The more of this const there is, the easier it is for the compiler and human to reason about.
I guess this is what WITH is for though.
I guess I should try putting those at the start of functions instead of var.




 - Jay 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://m3lists.elegosoft.com/pipermail/m3devel/attachments/20120901/017e7928/attachment-0001.html>


More information about the M3devel mailing list