Log in

View Full Version : Else if and script review of my crappy code


Pages : 1 [2]

feisty2
24th June 2020, 19:22
it's not about strict typing, also the term "strict typing" is ill-formed.
code without type declarations are more expressive since they automatically generalize to any type that meets the requirements, you define a "typeless" entity once

auto f(auto&& x) {
return x + x;
}

it automatically works for any compatible case

auto x = f(1); // x is of type int, x == 2
auto y = f(3.14); // y is of type double, y == 6.28
auto z = f("aa"s); // z is of type std::string, z == "aaaa";



if you declare the parameter type and return type for "f", say "int", it would fail with an std::string argument even if the logic totally works for such case. and the returned result with a double argument would be incorrect.



also, if you want something that either completely meets the requirements, or "kinda" meets the requirements and you wanna manually take care of the incompatible part for the latter case, this is also impossible with explicit type declarations.
say you want a function "g", that accepts an object as its argument, if the object has a member named "x", print x, otherwise print "error!"

auto g(auto&& obj) {
if constexpr (requires { &obj.x; })
std::cout << obj.x;
else
std::cout << "error!"
}

now how you gonna define something like this with type declarations??

Gavino
24th June 2020, 22:05
code without type declarations are more expressive since they automatically generalize to any type that meets the requirements, you define a "typeless" entity once

auto f(auto&& x) {
return x + x;
}

it automatically works for any compatible case

auto x = f(1); // x is of type int, x == 2
auto y = f(3.14); // y is of type double, y == 6.28
auto z = f("aa"s); // z is of type std::string, z == "aaaa";

Of course, you can do exactly that in the Avisynth language.
function f(x) { # or f(val x)
return x + x
}
will work for f(1), f(3.14) and f("aa") in just the same way as your code.

But most functions in real Avisynth scripts don't have such 'polymorphic' arguments, and therefore benefit from the automatic type-checking you get by explicitly typing the args.

StainlessS
25th June 2020, 01:07
There you are Feisty, Big G is on his game, quite impressive for an ol' fart, eh!
We ol' farts dont like learning new tricks, I mastered 'fetch the stick' but when it comes to 'fetch the frizbee' I just get confused.
You young scallywags will find the same in 40 or 50 years, when young coders will utter a dozen or so sentences and
they will have just written their holosuite masterpiece featuring a cast of billions and entire star systems to play around in.
(And you will be saying, "but does it support C++20 and 'auto'", and run on Windows 10 Ultimate).
Gavino and I, will not be saying very much at all by then, but we'll be waiting for you on the dark side, just a single step through the looking glass.
Unfortunately, Grouchy will have gone by another route, not the looking glass but the trap-door, sad but there you go, not all stories have a happy ending.

FranceBB
25th June 2020, 09:21
EDIT: PlanarRGB is Avs+ only.
EDIT: What colorspaces do your Cube whotsits work for ?

Indeed, it works in PlanarRGB 16bit and I was testing it under Avisynth 3.6.1 x86 on Win10 ('cause I'm technically at work and I shouldn't really post on Doom9, but hey, we use Avisynth, so I guess I'm fine with my conscience as I don't consider it playing around xD).
Anyway, I guess I just learned something new: I didn't know that Avisynth+ was able to use if statements just like C++. It's so much better to write plugins like this 'cause it's consistent with C++ behaviour and it's way easier to read. :D

As a double check against your statement, this old fashioned way of declaring conditionals works:


ColorBars(848, 576, pixel_type="YV12")

clp = last

clp.isYV12() ? Converttoyv16() : clp


and returns yv16()

and this new one C++-like works as well in Avisynth+:


ColorBars(848, 576, pixel_type="YV12")

clp = last

if (clp.isYV12() == true)

{
clp.Converttoyv16()

}


As to the beer, it's fine, as long as you'll reply here on Doom9, I'm gonna be happy with that. :)

Gavino
25th June 2020, 10:53
I didn't know that Avisynth+ was able to use if statements just like C++.
In case you don't know, I'll point out that you can also do this (and other language extensions) in plain Avisynth, by using GScript.
(GScript is effectively built-in to Avisynth+, with my permission)

if (clp.isYV12() == true)
...

As StainlessS has pointed out, there's no need to explicitly compare a boolean value with 'true' or 'false' - just use
if (clp.isYV12())