This shows you the differences between two versions of the page.
dss:laboratoare:02 [2019/06/18 12:52] razvan.nitu1305 [2. Generic Search] |
dss:laboratoare:02 [2019/06/20 10:40] (current) razvan.nitu1305 [2. Generic Partitioning Search] |
||
---|---|---|---|
Line 683: | Line 683: | ||
Template constraints have the same syntax as an if statement (without else). | Template constraints have the same syntax as an if statement (without else). | ||
+ | |||
+ | |||
===== Exercises ===== | ===== Exercises ===== | ||
+ | |||
+ | The exercises for ''lab-02'' are located in this [[https://github.com/RazvanN7/D-Summer-School/tree/master/lab-02|repo]]. | ||
==== 1. Enum disassembly ==== | ==== 1. Enum disassembly ==== | ||
Go to this [[https://godbolt.org/z/0RGPvB|link]]. You will find the disassembly of a code. Observe how the call | Go to this [[https://godbolt.org/z/0RGPvB|link]]. You will find the disassembly of a code. Observe how the call | ||
- | to function **sum** is translated in assembly code. | + | to function **sum** is translated in assembly code. Explain the output. |
==== 2. Generic Partitioning Search ==== | ==== 2. Generic Partitioning Search ==== | ||
Line 697: | Line 701: | ||
* The algorithm will receive an array/associative array and an element and returns the number of elements that are lesser than the element in the array/associative array list of keys. | * The algorithm will receive an array/associative array and an element and returns the number of elements that are lesser than the element in the array/associative array list of keys. | ||
* The element type may be struct, class or builtin type. | * The element type may be struct, class or builtin type. | ||
- | * Struct and builtin types have default ordering comparison, however classes do not. | ||
* Use templated function(s) and **template constraints** or **static if**s to implement the various cases. | * Use templated function(s) and **template constraints** or **static if**s to implement the various cases. | ||
- | * Use these [[https://dlang.org/phobos/std_traits.html|helper traits]].a | + | * Use these [[https://dlang.org/phobos/std_traits.html|helper traits]]. |
+ | |||
+ | ==== 3. Pragma(msg) ==== | ||
+ | |||
+ | Pragmas are compile time instructions used to pass or ask the compiler for specific information. **pragma(msg)** is used as a compile time debugging tool that allows printing of compile time known variables: | ||
+ | |||
+ | <code D> | ||
+ | class Foo {} | ||
+ | Foo a; | ||
+ | enum b = 2; | ||
+ | pragma(msg, "hello"); // prints "hello" at compile time | ||
+ | pragma(msg, Foo); // prints Foo at compile time | ||
+ | pragma(msg, a); // error, a cannot be read at compile time | ||
+ | pragma(msg, typeof(a)); // prints Foo at compile time | ||
+ | pragma(msg, b); // prints 2 at compile time | ||
+ | </code> | ||
+ | |||
+ | Use **pragma(msg)** inside the templated functions from the previous exercise. Observe the output when compiling the code. Explain the results. | ||
+ | |||
+ | ==== 4. Template Sequence Parameters ==== | ||
+ | |||
+ | Navigate to the ''4-template'' directory. Inspect the source ''template.d'' and try to understand what happens. What does the code do? How many instances of the **printArguments** function are created? | ||
+ | |||
+ | <note hint> | ||
+ | Use **pragma(msg)** to inspect the instantiation type(s). | ||
+ | </note> | ||
+ | |||
+ | ==== 5. Error ==== | ||
+ | |||
+ | Navigate to the ''5-error'' directory. Inspect and compile the code. What happens? Why? Solve the issue. | ||
+ | |||
+ | ==== 6. Standard library template example ==== | ||
+ | |||
+ | Follow this [[https://github.com/dlang/phobos/blob/master/std/algorithm/sorting.d#L317|link]]. That is the official D language standard library implementation of the **ordered** function. Read the documentation and the unittests, then try to understand the implementation. This is how real life meta-programming looks like. Ask the [[http://ocw.cs.pub.ro/courses/dss?&#team|lab rats]] about clarifications on any misunderstandings. |