Order independent declaration example
This commit is contained in:
@@ -44,7 +44,10 @@ main :: (): int
|
|||||||
|
|
||||||
// After we loop and reassign slice values
|
// After we loop and reassign slice values
|
||||||
// old static_array gets changed
|
// old static_array gets changed
|
||||||
for slice
|
//
|
||||||
*it = 2
|
// In this example, operator ';;' is used
|
||||||
|
// it inserts a new line, allows to write this
|
||||||
|
// example in a single line
|
||||||
|
for slice;; *it = 2
|
||||||
|
|
||||||
assert(static_array[2] == 2)
|
assert(static_array[2] == 2)
|
||||||
73
examples/order_independent_declarations.kl
Normal file
73
examples/order_independent_declarations.kl
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
/*
|
||||||
|
Biggest problems with C/C++ are the header files and the fact that
|
||||||
|
top level declarations are required to be ordered.
|
||||||
|
|
||||||
|
In C++ you simply cannot write code like this:
|
||||||
|
|
||||||
|
struct Asset{
|
||||||
|
Asset_Tag asset_tag;
|
||||||
|
};
|
||||||
|
|
||||||
|
enum Asset_Tag{
|
||||||
|
ASSET_SOUND,
|
||||||
|
ASSET_IMAGE,
|
||||||
|
};
|
||||||
|
|
||||||
|
Even though it makes more sense to do that, I would rather first learn
|
||||||
|
about what Asset is rather then what tags it can have. C/C++ force on you
|
||||||
|
an order of declarations that doesn't match what you have in your head.
|
||||||
|
|
||||||
|
The next problem are the header files. Frequently on top of writing code,
|
||||||
|
in C/C++ there is a need to maintain a header file. Header files contain
|
||||||
|
all the forward declarations of the functions that you are implementing.
|
||||||
|
Problem with this is that if you are changing something, potentially
|
||||||
|
you now need to change that in multiple places. It would be nice if I could
|
||||||
|
write a module that doesn't yet have any concrete spec but the functions
|
||||||
|
are available everywhere, immediately. On top of that those functions would
|
||||||
|
be nicely localized in a single file without thinking about what depends
|
||||||
|
on what, how to fit it in the stack of includes etc. It would be just nice
|
||||||
|
to tell the compiler to solve all those issues for us.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// We can have main at the top level, this is something
|
||||||
|
// that is sometimes pretty tricky in C++
|
||||||
|
main :: (): int
|
||||||
|
// Even though Asset is beneath main, this still works
|
||||||
|
// this feature is even nicer when we think about modules etc.
|
||||||
|
asset1 := make_sound()
|
||||||
|
asset2 := make_sound()
|
||||||
|
asset1.next_asset = &asset2
|
||||||
|
|
||||||
|
// @todo: This does not work
|
||||||
|
// asset := []Asset{make_sound(), make_sound()}
|
||||||
|
// asset[0].next_sound = &asset[1]
|
||||||
|
|
||||||
|
|
||||||
|
// Recursion is a pain point for this kinds of algorithms
|
||||||
|
// As can be seen here it works very nicely
|
||||||
|
make_sound :: (should_exit_recursion: Bool = false): Asset
|
||||||
|
if should_exit_recursion == true
|
||||||
|
asset: Asset
|
||||||
|
asset.tag = Asset_Tag.Sound
|
||||||
|
return asset
|
||||||
|
return make_sound(true)
|
||||||
|
|
||||||
|
Asset :: struct
|
||||||
|
tag: Asset_Tag
|
||||||
|
|
||||||
|
// Pointers to self work as expected
|
||||||
|
// this is always a pain point for these kinds of
|
||||||
|
// algorithms
|
||||||
|
next_asset: *Asset
|
||||||
|
|
||||||
|
// enum #flag assigns default values differently from normal enums.
|
||||||
|
// It assigns a unique bit of a value to each enumeration.
|
||||||
|
// Default values go: 1, 2, 4, 8, 16, 32, 64
|
||||||
|
Asset_Tag :: enum #flag
|
||||||
|
Sound
|
||||||
|
Image
|
||||||
|
Text
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
2
main.cpp
2
main.cpp
@@ -225,7 +225,7 @@ int main(int argument_count, char **arguments){
|
|||||||
|
|
||||||
Scratch scratch;
|
Scratch scratch;
|
||||||
Array<OS_File_Info> examples = os_list_dir(scratch, "examples"_s);
|
Array<OS_File_Info> examples = os_list_dir(scratch, "examples"_s);
|
||||||
compile_file("examples/arrays_and_slices.kl"_s, COMPILE_AND_RUN);
|
compile_file("examples/order_independent_declarations.kl"_s, COMPILE_AND_RUN);
|
||||||
// For(examples){
|
// For(examples){
|
||||||
// if(it.is_directory) continue;
|
// if(it.is_directory) continue;
|
||||||
// compile_file(it.absolute_path, COMPILE_AND_RUN);
|
// compile_file(it.absolute_path, COMPILE_AND_RUN);
|
||||||
|
|||||||
Reference in New Issue
Block a user