s |
focus search bar ( enter to select, ▲ / ▼ to change selection) |
g e |
go to examples |
g m |
go to modules |
g o |
go to overview |
g r |
go to reference |
h |
toggle this help ( esc also exits) |
#modify
f :: (a: $A) -> $B #modify (A: ^Type_Info) -> <something> { <modify-code> } { <procedure-code> }
- provide a function to manipulate a type variable prior to it being used in a polymorphic procedure. 1 2
#modify
injects a user-defined procedure at compile time to allow for validation, modification, or other arbitrary actions on the type variables being passed to a polymorhpic procedure.
The procedure can be defined inline:
foo :: (f: $T) -> $R
#modify (T : ^Type_Info, R : ^Type_Info) -> ^Type_Info, ^Type_Info { /* .. */ }
{
// ..
}
Or named and shared:
ensure_R_is_big_enough :: (T : ^Type_Info, R : ^Type_Info) -> ^Type_Info, ^Type_Info { /* .. */ }
foo :: (f: $T) -> $R
#modify ensure_R_is_big_enough
{
// ..
}
bar :: (b: $T) -> $R
#modify ensure_R_is_big_enough
{
// ..
}
Multiple #modify
procedures can be chained, and execute in order.
foo :: (a: $T) -> $R
#modify ensure_R_is_big_enough
#modify reject_R_if_float
{
// ..
}
// from: https://youtu.be/BwqeFrlSpuI?t=2871
// and: https://youtu.be/7Fsy2WaxLOY?t=109
// and: https://youtu.be/7Fsy2WaxLOY?t=355
sum :: (a : [] $T) -> $R // note that return type is different from T
#modify ensure_R_is_big_enough
{
result : R = 0;
for a result += it;
return result;
}
fill_and_sum :: (a : [] $T) -> $R
#modify ensure_R_is_big_enough // using same modify proc
{
for 0..a.count-1 a[it] = cast(it*10, T);
return sum(a);
}
ensure_R_is_big_enough :: (T : ^Type_Info, R : ^Type_Info) -> ^Type_Info, ^Type_Info {
if T.type == Type_Info_Tag.FLOAT return T, T; // handle float32 and 64 at their own precisions
if T.type != Type_Info_Tag.INTEGER {
printf("#modify proc ensure_R_is_big_enough only takes int or float types.\n");
return null, null;
}
// for integer types, ensure return type won't overflow
info := cast(T, ^Type_Info_Integer);
if info.size_in_bits >= 32 return T, T; // big enough
if info.signed return T, type_info(s32);
return T, type_info(u32);
}
[..] this thing called a modify directive [..] you can attach it onto a polymorphic procedure, and it supplies code that runs at compile time that maps the types given by the user onto the types of the generated function.
“Polymorphic Procedures, part 1” YouTube, uploaded by Jonathan Blow, Apr 1, 2015, https://youtu.be/BwqeFrlSpuI?t=2593 ⮌
..you can chain them together, and if you use more than one, they get called in order. So, [..] you could have a toolkit of them that you just apply.
“Polymorphic Procedures, part 2” YouTube, uploaded by Jonathan Blow, Apr 1, 2015, https://youtu.be/7Fsy2WaxLOY?t=423 ⮌
jailang
2019 pixeldroid
https://github.com/pixeldroid/jailang |
programming pages theme v0.5.21 (https://github.com/pixeldroid/programming-pages) |
s |
focus search bar ( enter to select, ▲ / ▼ to change selection) |
g e |
go to examples |
g m |
go to modules |
g o |
go to overview |
g r |
go to reference |
h |
toggle this help ( esc also exits) |