D course material

Andre Pany andre at s-e-a-p.de
Wed Mar 14 08:53:17 UTC 2018


On Tuesday, 13 March 2018 at 12:39:24 UTC, Dmitry Olshansky wrote:
> Hi, folks!
>
> I’m testing waters for a D course at one University for first 
> time it’ll be an optional thing. It’s still discussed but may 
> very well become a reality.
>
> Before you ask - no, I’m not lecturing and in fact, I didn’t 
> suggest D in the first place! Academics are finally seeing 
> light in the gloom of 1 year OOP in C++ course having 
> underwhelming results.
>
> Now to the point, I remeber Chuck Allison (pardon if I 
> misspelled) doing D lectures at Utah Valley University, here:
> https://dconf.org/2014/talks/allison.html
>
> There is also Ali’s book. But anything else easily adoptable as 
> course material?
>
>> Dmitry Olshansky

Hi Dmitry,

for presenting D to my team I used following example. It 
highlights
some features of D: Meta programming, templates, CTFE, UFCS, OOP 
in D,
Functional programming in D and ...

It is a compile time i18n library in ~50 lines.

import std.experimental.scripting;

const devBundle = import("messagebundle.properties");
const identifiers = getIdentifiers(devBundle);
const languages = ["en", "de"];

void main()
{
     auto t = new TextBundle("en");
     writeln( t.text!"task.isNotARegisteredCommand"("lala"));
}

string[] getIdentifiers(string s)
{
     return s.split("\n").filter!(l => l.canFind(" = ")).map!(l => 
l.split(" = ")[0]).array;
}

class TextBundle
{
     private string _language;

     alias TextKeyValue = string[string];
     private static const TextKeyValue[string] 
_languageTextKeyValue;

     this(string language)
     {
         assert(language in _languageTextKeyValue);
         _language = language;
     }

     string text(string s)(string[] params...)
     {
         static assert(identifiers.canFind(s));
         string content = _languageTextKeyValue[_language][s];

         foreach(i, param; params)
             content = content.replace("{"~i.to!string~"}", param);

         return content;
     }

     static this()
     {
         static foreach(language; languages)
         {
             static if( __traits(compiles, 
import("messagebundle_"~language~".properties")))
             {
                 string s = 
import("messagebundle_"~language~".properties");
                 foreach(a; s.split("\n").filter!(l => l.canFind(" 
= ")).map!(l => l.split(" = ")))
                 {
                     _languageTextKeyValue[language][a[0]] = a[1];
                 }
             }
         }
     }
}

The property files:

-messagebundle.properties-
task.isNotARegisteredCommand = `{0}` is not a registered command.

-messagebundle_en.properties-
task.isNotARegisteredCommand = `{0}` is not a registered command.

The property file for DE does not exist for showing 
__traits(compiles,...) functionality.
I does fit in a 60 minute lecture.

Kind regards
André






More information about the Digitalmars-d mailing list