Stop Implicit Type Conversion in Coder

조회 수: 4 (최근 30일)
Abhishek Kumar
Abhishek Kumar 2018년 3월 8일
댓글: Thomas Blazek 2018년 3월 8일
Hello All
As per ISO 26262, for ASIL B to ASIL D, there must not be an implicit data type conversion. But in MATLAB, we can have
testValue = uint8(2);
testValue = -126; % It is perfectly fine but shall throw a warning or error
Is there anyway to throw a compile time warning here? Suggestions are welcome.

답변 (1개)

Thomas Blazek
Thomas Blazek 2018년 3월 8일
So, MATLAB is dynamically typed. That means, by assignment, you overwrite the type. It is not a type conversion, it is literally setting a new type. And, MATLAB is very liberal about it. Furthermore, it is interpreted, not compiled, therefore no compile-time checks are possible.
That said, there is a workaround if you really want to be sure. You can Define the following Class
classdef Uint8Class
properties
value = []
end
methods
function obj = set.value(obj,in)
if in >= 0 && in < 256
obj.value = in;
else
error('Not a Uint8')
end
end
function out = get.value(obj)
out = uint8(obj.value);
end
end
end
x = Uint8Class;
x.value = 3; % works
display(x.value)
x.value = -2 % Raises an error
The way this works is that it overrides get and set methods of "x". Then, you can write your own checks on assignment. The downside is that you have to address the value by "variable.value" instead of just "variable", but it does allow for checks as you want them, and even "fails in a meaningful way".
  댓글 수: 2
Abhishek Kumar
Abhishek Kumar 2018년 3월 8일
I understand that MATLAB is a dynamically typed language but he question here is regarding a standard which MATLAB Supports (ISO 26262). So if we use MATLAB/Simulink Check, is it possible to get an Error without changing the Class Definition. I would not want to create a DataType Class anyway. Your workaround is quite smart though.
Thomas Blazek
Thomas Blazek 2018년 3월 8일
Ah ok, sorry I didn't quite catch that, in that case I do not have an idea, Sorry.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 IEC Certification Kit (for ISO 26262 and IEC 61508)에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by