How can define enumerator in Methods? - plc

There is a problem when u define an enum in a method.
I was trying to do this:
VAR
enumA:(A,B,C);
END_VAR
and there is the compiler reaction when I used this in TwinCAT3 Shell (TcXaeShell).
any help would be appreciated.

You can only use global enumerations in methods. It's one of the limitations with local enumerations.
https://alltwincat.com/2021/11/16/local-enumerations/

You should first define variable type as enumeration in DUT
TYPE MyEnum:
(A, B, C)
END_TYPE
Then in a program you can declare variable of that type
VAR
enum: MyEnum;
END_VAR
Inside the program if you want to compare it.
IF enum = MyEnum.C THEN
// Do something
END_IF;

I’ve run into this issue before. You must declare the local enumeration in the variables section of the function block. Then you can use it in the methods of the function block.

Related

Use global class object from one module in other in Delphi

I want one class object to be a global variable in one module in Delphi, and other modules(.pas-files, maybe it's not correct to call it a module, I'm not really familiar with Delphi) be able to use this global variable. Is it a way to initialize global object not inside some global function and have other modules use this global object directly? Without any global functions. Is it possible?
Simply declare the variable in the unit's interface, and then use that unit in other units' uses clauses as needed. You can initialize the variable in its unit's initialization section.
UnitA.pas:
unit A;
interface
type
TMyClass = class
// members as needed...
end;
var
GlobalObj: TMyClass;
implementation
// implement TMyClass methods as needed...
initialization
GlobalObj := TMyClass.Create;
finalization
GlobalObj.Free;
end.
UnitB.pas:
unit B;
...
uses
..., A;
// use A.GlobalObj as needed...
end.

Error "Inout argument could be set to a value with a type other than ..." when passing in a protocol type

I am coming from C++ to Swift. I have this situation with respect to protocols and structs (I am working with random numbers):
(1) A protocol RandomPr that specifies there are methods such as randFloat() that returns a Float between 0 and 1.
(2) A struct RandomS that implements RandomPr using a "real" random number generator.
(3) A struct FakeRandomS that implements RandomPr, but has additional methods such as loadFloat() to load an array of Floats that are then regurgitated when I call randFloat() on a FakeRandomS instance. (For testing purposes.)
Now I have a function DoSomething(rng: inout RandomPr), which I want to use with both RandomS and FakeRandomS. The parameter has to be in-out as I need to update the RNG (whether it is real or fake). No problem calling DoSomething with an instance of RandomS.
But if I do
var fakeRng = FakeRandomS()
fakeRng.loadFloat([0.1, 0.2, 0.3])
DoSomething(rng: &fakeRng)
I get an error "Inout argument could be set to a value with a type other than 'FakeRandomS'. The suggested fix is to define fakeRng as
var fakeRng: RandomPr = FakeRandomS()
But now trying to call loadFloat on fakeRng fails because RandomPr doesn't have a method loadFloat, and the compiler doesn't see that fakeRng does have one.
I tried making another protocol FakeRandomPr that contains the extra methods that FakeRandomS has, and defining
var fakeRng: RandomPr & FakeRandomPr = FakeRandomS()
but, frustratingly, I now get the "Inout argument could be set to a value with a type other than ..." error back again.
I could do the equivalent of this in C++ without problems (a pure abstract base class and two child classes, passed in by reference), and it didn't matter that one of the things I wanted to pass in has extra methods. I want to translate this into protocols and structs. What is the Swift solution?
What is the Swift solution?
You may need to use generics:
func doSomething<RP: RandomPr>(rng: inout RP) {
//...
}
Swift is not just another syntax of C++, better think in Swifty way.

how to use function in systemverilog?

I found sentencenlike this.
function device check_device ;
case ..
in system verilog code.
The device are consist of enum typedef.
Also check _device is nothing.
Does anyone know what is it? Could you please explain this?
Also Why does -> event exist except #?
What are different between them?
Given the code snippet, check_device is the name of the function you are defining. It would return a value of type device, which as you said is typedefed as an enum definition.
In SystemVerilog, you can declare an explicit event and wait on that. The operator -> is used to trigger an explicit event. Operator #, as in Verilog, is there to let you wait for an event.
For example:
class Foo;
event bar;
// ...
function void notify_bar;
->bar;
endfunction;
task wait_for_bar;
#bar;
endtask;
endclass

What's the proper way to declare a field holding a function in dart?

Imagine a silly class like this:
class ConditionalWorker{
var validityChecker= (inputs)=>true;
ConditionalWorker(this.validityChecker)
...
Now my question is, what is the proper way of declaring the validityChecker field?
This tutorial suggests using typedefs. But that's not very practical. Firstly it's a chore to write a lot of typedefs that would only be used once. And secondly these typedefs show up and pollute the autocompletion of my IDE.
The var works best, with custom setters/constructor arguments to keep it always of a specific kind, but I know it's discouraged by the style guide.
I could do Function<bool> but that just a more glorified var and the amount of work is the same.
It's a shame because it's perfectly legal to have a function like this:
bool every(bool test(E element));
where the parameter is a very well defined function, but I can't have a field declared the same way:
bool test(E element);
But hopefully there is something just as good that I didn't figure out. Right?d
If you want a function type more specific than Function, you need a typedef.
If you don't like to have named typedefs for every return type, you can define generic function types yourself.
typedef R function0<R>();
typedef R function1<S,R>(S arg1);
typedef R function2<S,T,R>(S arg1. T arg2);
typedef R function3<S,T,U,R>(S arg1, T arg2, U arg3);
Then you can write:
function1<int,int> curryAdd(int x) => (int y) => x + y;
Or if function0 looks bad to you, you can name them NullaryFunction, UnaryFuncytion, BinaryFunction, TernaryFunction, or any other name that you like.
If Function<bool> is not specific enough (you also want to specify the number and type of the arguments you have to use typedefs. There are no other ways.
I'm not sure why you think it is not practical. If you want to specify the type for a field that references a value you have to use one of the existing classes or create a new one. It's the same for fields referencing functions.
With Dart 2 we can use inline function types and we need to use it instead of typedefs where possible.
With inline function types we now can define a function as a field or a property as simple as this:
final bool Function(E) test;

Scala: Why is it necessary to assign values to a var/val during declaration

Unless I've been doing it wrong. It doesn't seem like we can do things like:
var x;
x = 1;
in Scala, but rather you have to declare and assign a value to it. Are there any reasons for why this is the case?
The obvious reason is to help not leave variables uninitialized.
Note that in your declaration without initialization, you will also need to specify the type.
var x: Type;
gives the following error:
only classes can have declared but undefined members (Note that variables need to be initialized to be defined)
Actually only abstract classes can declare members without defining them. You can still get the desired behavior (variables initialized to a default value) as
var x: Type = _
If Type is a reference type, x will be null. This scenario is useful, for example, in case where a factory method completes initialization of an object after object construction.