Vala code with static struct dont works after update to 0.44 - vala

Just did a standard job interview is to calculate the volume of water in the histogram. On Monday this code worked, and still works on this site. After updating vala, an error is now displayed.
UPD: more easy example
> Algoritm.vala:2.5-2.16: error: struct `Algotitm.first' cannot be empty
> struct first {
> ^^^^^^^^^^^^ Algoritm.vala:6.5-6.17: error: struct `Algotitm.second' cannot be empty
> struct second {

When posting a question on Stack Overflow it is always good to post an example of the code that is a minimum, complete and verifiable example.
From the link you've provided it appears you have a struct with only members marked as static:
struct First {
static int data;
static int pos;
}
void main () {
}
Marking the fields as static means they aren't instance fields and so the struct is empty of fields. That's why you are getting the error message about the struct being empty. I'm not sure Vala should even allow marking struct fields as static, but it does make sense to allow methods in structs to be static.
You need to remove the static modifiers. This will work:
struct First {
int data;
int pos;
}
void main () {
}
Update
I'm guessing you are trying to write performance optimized code and you think static helps with that. static in Vala means there is no instance data to use. If you are using a data structure like a class or struct then it only makes sense to have instances of those. If you want something to remain unchanged during the running of your program use const in a namespace.
Using a struct may give you a slight performance boost if your are using a very large number in your program. structs created in Vala are allocated on the stack instead of the heap so may be slightly faster. If you are passing structs around you may want to consider [SimpleType] attribute. That means structs will be passed by value in C as well as Vala. Without the [SimpleType] they are copied and passed by reference at the C level, which appears as copy by value in Vala.
Structs in Vala can have initializers (similar to a constructor for a class) and methods. So what I can extract from your second pastebin you could write that as:
struct First {
int data;
int pos;
public First (int[] mass) {
data= 5;
pos = mass.length;
}
public int sas () {
return data + pos;
}
}
void main () {
int[] a = {1,3,0,1,2,3,2,1};
var b = First (a);
print (#"$(b.sas ())\n");
}
That is a follow on question though and should be asked as a second question on Stack Overflow. This is a public forum that follows a format that allows other people to learn from the question and answers.

Related

In swift, using final or private class to increase performance is more worth than using struct?

I read this link. (https://developer.apple.com/swift/blog/?id=27)
I'm wondered if we use final or private class for static dispatch, class is extremely limited to inheritance(I think this is why we use class).
in the first place, Apple recommend struct except two situation in this documentation.
(https://developer.apple.com/documentation/swift/choosing-between-structures-and-classes)
So, can someone come up with an example of what might lead to making a final class instead of struct?
These are two completely separate topics.
The first question is one of static vs dynamic dispatch. But this has nothing to do with the class vs struct question. Instead it is a discussion of the tradeoff the flexibility of dynamic dispatch vs the performance of static dispatch. Admittedly, that article you reference happens to be focusing on the static/dynamic dispatch implementation issues for a class. But we have very similar static vs. dynamic dispatch questions for struct types within the protocol oriented programming (POP) paradigm, too. (See below.)
Whether dealing with value types or reference types, it is the exact same dynamic/static dispatch trade-off: Is the performance gain essential and is the loss of flexibility warranted?
The second one is about value semantics vs reference semantics. I think the classic “Crusty” WWDC 2015 video is a great primer on this topic. And while I think your link is as excellent, that old WWDC video also has a discussion of When to use classes.
In short, Apple advises using value types where appropriate (in order to prevent unintended sharing, mitigate thread-safety synchronization issues, etc.) and that we use static dispatch where essential. But one has largely nothing to do with the other.
And before you ask, here is an example of static vs dynamic dispatch with a struct.
First, static dispatch:
protocol P {
}
extension P {
func f() {
print("P implementation")
}
}
struct S: P {
func f() {
print("S implementation")
}
}
let p: P = S()
p.f() // “P implementation”
And dynamic dispatch:
protocol P {
func f()
}
extension P {
func f() {
print("P implementation")
}
}
struct S: P {
func f() {
print("S implementation")
}
}
let p: P = S()
p.f() // “S implementation”

Not able to understand (struct Stack* stack)

struct Stack* create(int max) {
struct Stack *stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->maxSize = max;
stack->top = -1;
stack->array = (int*)malloc(stack->maxSize * sizeof(int));
return stack; }
int isFull(struct Stack* stack)
{
if(stack->top == stack->maxSize - 1)
{
printf("STACKOVERFLOW\n");
}
return stack->top == stack->maxSize - 1;
}
What is the use of struct Stack* stack in this question?
I am trying to understand stack but can't understand why dow we have to use this struct Stack* stack
The code is dynamically allocating the internal array that it is (presumably*) using as a stack. This keeps the implementer from having to predefine one or more fixed length arrays which would be wasteful. The use of a dynamically allocated data structure makes the code flexible. Presumably this is straight C code and the implementation of the code as a struct gives you advantages approaching that of an actual object in an actual object oriented language (partial [but not real] data hiding for example).
*I have to say presumably because the code is nor implementing any definite stack operations aside from checking if the stack is full (isFull() ).

Dart. Late initialize final variables

Is there way to late initialize for final variables. The problem is many values initialized with entry point to the class, which is not constructor. Hence they cannot be final right now. But in scope of particular class they will not be changed. For ex.
Controller controller;
double width;
void setup(final itemWidth) {
controller = MyController();
width = itemWidth;
}
Could it be possible? Right now I see only solution as a annotation. You might think it's for visual effect. But in fact it helps to avoid unpredictable flow during testing.
It is now possible to late initialize variables. For more information see Dart's documentation. The text below is copied from Dart's documentation:
Late final variables
You can also combine late with final:
// Using null safety:
class Coffee {
late final String _temperature;
void heat() { _temperature = 'hot'; }
void chill() { _temperature = 'iced'; }
String serve() => _temperature + ' coffee';
}
Unlike normal final fields, you do not have to initialize the field in its declaration or in the constructor initialization list. You can assign to it later at runtime. But you can only assign to it once, and that fact is checked at runtime. If you try to assign to it more than once — like calling both heat() and chill() here — the second assignment throws an exception. This is a great way to model state that gets initialized eventually and is immutable afterwards.

how do i declare variables, compare them and then use them inside a function

i am developing an ea that requires me to compare the high of previous 2 bars and whichever one is higher, use that as a stop loss value.
same for opposite side trades, i need to compare previous 2 lows and use the lower one as stop loss value.
what i am doing is this:-
void onTick()
{
static int ticket=0;
double ab=(//calculation for ab);
double de=(//calculation for de);
if(Low[1]<Low[2])
double sll=Low[1];
if(Low[1]>Low[2])
double sll=Low[2];
if(buy logic comes here)
{
double entryPrice=////////;
double stoploss=sll-xyz;
double takeprofit=entryPrice+((entryPrice-stoploss)*3);
ticket = OrderSend(Symbol(),...entryPrice,stoploss,takeprofit,.....);
}
if(ticket == false)
{
Alert("Order Sending Failed");
}
}
the problem is i am not able to reference the values of sll and get an error message saying "sll undeclared identifier"
i am fairly new to programming and would appreciate if someone can help me out with this.
I have added most of the code for you to understand the logic.
you would have to declare them outside the scope of the if statements if you want to use variables anywhere else so instead of doing that take a look at this
double sll; // declare sll outside the if statements
if(Low[1]<Low[2])
sll=Low[1];
if(Low[1]>Low[2])
sll=Low[2];
if(buy logic comes here)
{
bool res = OrderSend(..........);
}
Judging by what you wrote, it looks like you may be using res somewhere else too which then you need to define outside of the if statement because scoping.

Shared instance of c file in objective c?

I am working on a chess engine in C/Objective-C and I rewrote a large part of my engine in straight-c to improve the speed. My question is, I have about 3KB of tables I initialize in my C file that I don't want to reinitialize every time a function from this file is called. If this were a regular objective-c class I would create a shared instance. However, the core of my engine is in two .h and a .c files. Should I make all of the tables used by my engine static? Will they persist between multiple other files calling functions in my engine? Should I make a static struct to hold my tables? I'm not sure what the best approach is here. Thanks!
Example:
Test.h:
int getInt(int index);
Test.c:
static int integers[4] = {1,2,3,4};
int getInt(int index) { return integers[index]; }
Every time I call getInt from another file, will it reallocate 'integers'? Or will it reuse the same array? I want to prevent it from unnecessarily reallocating a bunch of static arrays.
Ok, what you did was accessor on a static variable...
A static is only initialized once, so it does get initialized only once per launch.
You can keep it this way, or change it to a global to access it without calling function.
This code could typically get inlined, so changing it to a global is more a matter of taste than performances.
Edit: short summary on allocations
static int array[] = {1, 2}; // Allocated and initialized once
int array2[] = {1, 2, 3}; // Allocated and initialized once
int function() {
int array3[] = {1, 2, 3}; // Allocated and initialized at every call of function();
static int *array4 = malloc(42); // Allocated and initialized once
int *toto = malloc(42); // Allocated at every call of function();
}

Resources