OpenSCAD 使用者手冊/型別測試函式
外觀
[注意: 需要版本 2019.05]
is_undef 接受一個引數。如果引數未定義,此函式返回 true。如果引數未定義,則返回 false。當檢查變數(如 `is_undef(a)`)時,它會靜默地執行變數查詢,這意味著 is_undef(a) 不會導致 `WARNING: Ignoring unknown variable 'a'.`
另一種方法是使用以下程式碼
if(a==undef){
//code goes here
}
或者
b = (a==undef) ? true : false;
會導致
WARNING: Ignoring unknown variable 'a'.
is_undef 也適用於特殊變數,允許進行以下操作
exploded = is_undef($exploded) ? 0 : $exploded; // 1 for exploded view
對於舊版本的 openscad,可以使用以下方法模擬 is_undef
function is_undef ( a ) = (undef == a) ;
這當然會導致警告,但不需要更改依賴於 is_undef() 的程式碼。
[注意: 需要版本 2019.05]
echo("returning true");
echo(is_list([]));
echo(is_list([1]));
echo(is_list([1,2]));
echo(is_list([true]));
echo(is_list([1,2,[5,6],"test"]));
echo("--------");
echo("returning false");
echo(is_list(1));
echo(is_list(1/0));
echo(is_list(((1/0)/(1/0))));
echo(is_list("test"));
echo(is_list(true));
echo(is_list(false));
echo("--------");
echo("causing warnings:");
echo(is_list());
echo(is_list(1,2));
[注意: 需要版本 2019.05]
echo("a number is a number:");
echo(is_num(0.1));
echo(is_num(1));
echo(is_num(10));
echo("inf is a number:");
echo(is_num(+1/0)); //+inf
echo(is_num(-1/0)); //-inf
echo("nan is not a number:");
echo(is_num(0/0)); //nan
echo(is_num((1/0)/(1/0))); //nan
echo("resulting in false:");
echo(is_num([]));
echo(is_num([1]));
echo(is_num("test"));
echo(is_num(false));
echo(is_num(undef));
[注意: 需要版本 2019.05]
echo("resulting in true:");
echo(is_bool(true));
echo(is_bool(false));
echo("resulting in false:");
echo(is_bool([]));
echo(is_bool([1]));
echo(is_bool("test"));
echo(is_bool(0.1));
echo(is_bool(1));
echo(is_bool(10));
echo(is_bool(0/0)); //nan
echo(is_bool((1/0)/(1/0))); //nan
echo(is_bool(1/0)); //inf
echo(is_bool(-1/0)); //-inf
echo(is_bool(undef));
[注意: 需要版本 2019.05]
echo("resulting in true:");
echo(is_string(""));
echo(is_string("test"));
echo("resulting in false:");
echo(is_string(0.1));
echo(is_string(1));
echo(is_string(10));
echo(is_string([]));
echo(is_string([1]));
echo(is_string(false));
echo(is_string(0/0)); //nan
echo(is_string((1/0)/(1/0))); //nan
echo(is_string(1/0)); //inf
echo(is_string(-1/0)); //-inf
echo(is_string(undef));
[注意: 需要版本 2021.01]
is_function 檢查僅適用於表示式,因此它可以應用於函式文字或包含函式的變數。它不適用於內建函式或普通函式定義。
echo(is_function(function(x) x*x)); // ECHO: true
func = function(x) x+x;
echo(is_function(func)); // ECHO: true
function f(x) = x;
echo(is_function(f)); // WARNING: Ignoring unknown variable 'f' / ECHO: false