I am New to The Vala Programming Language and trying to make a program in The Vala Programming Language and i need to have random number.
How Would I Go About Do that?
You can use build in GLib func GLib.Random.int_range (int32 begin, int32 end)
Also you can find many examples here.
Related
It's more of a technical question, not really an issue. Since we don't have variadic functions in cgo and there's currently no valid solution, I wonder if it'd be possible to cast interface{} to cgo types. So this would allow us to have more dynamic functions. I'm pretty sure we're not even allowed to assign types in a dynamic way to arguments in exported (//export) functions, neither the use of ellipsis is allowed. So what's the reason behind all those limits?
Thanks for answering.
import "C"
//export Foo
func Foo(arg1, arg2, arg3) {
}
C compilers are allowed, but not required, to return different types using different return mechanisms. For instance, some C compilers might return float results in the %f0 register, double results in the %f0:f1 register pair, integer results in the %d0 register, and pointer results in the %a0 register.
What this means for the person writing the Cgo interface for Go is that they must, in general, handle each kind of these C functions differently. In other words, it's not possible to write:
generic_type Cfunc(ctype1 arg1, ctype2 arg2) { ... }
We must know, at compile time, that Cfunc returns float/double/<some-integer-type>/<some-pointer-type> so that we can grab the correct register(s) and stuff its (or their) value(s) into the Cgo return-value slot, where the Cgo interface can get it and wrap it up for use in Go.
What this means for you, as a user of a Go compiler that implements Cgo wrappers to call C functions, is that you have to know the right type. There is no generalized answer; there is no way to use interface{} here. You must communicate the exact, correct type to the Cgo layer, so that the Cgo layer can use that exact, correct type information to generate the correct machine code at compile time.
If the C compiler writers had some way of flagging their code so that, e.g., at link time, the linker could pull in the right "save correct register to memory" location, that would enable the Cgo wrapper author to use the linker to automagically find the C function's type at link time. But these C compilers don't offer this ability to the linkers.
Is your particular compiler one of these? We don't know: you didn't say. But:
I'm pretty sure we're not even allowed to assign types in a dynamic way to arguments in exported (//export) functions, neither the use of ellipsis is allowed. So what's the reason behind all those limits?
That's correct, and the (slightly theoretical) example above is a reason. (I constructed this example by mixing actual techniques from 68k C compilers and SPARC C compilers, so I don't think there's any single C compiler like this. But examples like this did exist in the past, and SPARC systems still return integers in %o0, or %o0+%o1 on V8 SPARC, vs floating point in %f0 or %f0+%f1.)
I am working on a project right now, and I would greatly enjoy being able to extend a cross compiler to convert some code into other languages. For example, I might have an AST of some code, and I would like to pass that off to a cross compiler with the intended language and receive some code in the language specified in return.
So to sum it up: is there any extensible cross compiler that I can just give an AST or equivalent and receive code in return?
(I know about Haxe, but the compiler is not very extensible and I would prefer to not transpile)
I have made the decision to use LLVM as the native compiler, and will write my own custom transpilers to other languages, as I could find no other decent option. If you would like to follow my project, head over to Provalang.
Is there a way to write a front-end for my own, invented language?
Let's say I know regular expressions, yacc, lex and stuff, I want only to know how to bind it to existing GCC. I want something like:
%% This is a comment
%% This is source of XD programming language
troll x = 1; %% 'troll' is an automatically guessed type
x+laugh[]; %% Will print some laughs and format the hard drive
Here, the + represents C/C++ structure/class member operator, the square brackets are function call arguments. Semicolon means the same as in C.
How could I bind it so that if i entered
gcc -o app app.troll
it would compile my code, possibly with a similar C intermediate (anyways, the comments would probably be absent):
// The type has been guessed... It's int!!!
int x = 1;
// We can't call a method on an `int`, so we make it an external one
__trollvm_laugh_int (x);
And finally, it would be converted to GENERIC/GIMPLE, optimized and dumped to assembly.
I have seen two approaches:
G++ (GNU C++ Compiler, part of GCC). It compiles directly to GENERIC/GIMPLE
GPC (GNU Pascal Compiler) - translates code to C, then eventually calls GCC
If anyone knows anything, please let me know. Any help appreciated.
In Pascal it is possible to declare union types:
AnimalType = (Dog, Cat);
Animal = record
name: string;
case myType: AnimalType of
Dog: (weight: Integer);
Cat: (age: Integer);
end;
However, it is easy to violate the contract of case:
var
a: Animal;
begin
a.name := 'Kittie';
a.myType := Cat;
a.weight := 10; // There is no weight for cats!
writeln(a.age); // Prints 10
end.
There is a semantic error in this example, but the compiler typechecks it successfully. Also, there is no error at the runtime.
So, does the case block exist only for the documentation purposes?
The short answer to your question is, "no case blocks in variant records are not there only for documentation purposes". I say this because although the Pascal implementation you are using did not detect the fact that the program as accessing an inactive variant, other implementation do detect this error.
The long answer to your question is as follows.
Many people who are just learning Pascal don't realize that there are two major flavours of the Pascal language. There is ISO 7185 Standard Pascal (or just Standard Pascal for short) and there is Turbo/Borland Pascal (the most popular variant). So let me give you two answers to your question "So, does the case block exist only for the documentation purposes"?
The Standard Pascal Answer
Standard Pascal defines an error as "A violation by a program of the requirements of this International Standard that a processor is permitted to leave undetected". So yes, the program you have given does contain an error and yes the processor (i.e. Pascal implementation) you are using does not detect it, but other implementation will detect it, so no the case block in variant records actually has a functional purpose.
The Turbo/Borland Answer
As far as the Turbo/Borland Pascal flavours go, I don't know if none of them will detect this error, but even if none of them do, it's probably better to think of this as an error that they do not detect rather than as something for documentation purposes only. Saying something is for documentation purposes only, sounds to me like saying it was never intended to be functional.
A union makes it possible to give a variable multiple types. In your example, weight and age are stored at the same location in memory.
If you want a 'typesafe union', you should use inheritance.
Is there a way to use a for loop as a condition? Something like this?
if((for(int x = 0; x < 5; x++){if(x == 2){return true;}return false;}) == true)
Edit:
Another example
if(for(int x = 0; x < 5; x++) == 2)
I just wanted to know if it could be done. I expect that Blagovest Buyukliev and marzapower answers are accurate, based on my question. Thank you SO for you helpful responses.
That wouldn't make much sense, as C-ish loops are just execution control structures. There's no type that you can say loops in general have.
From your examples, what it looks to me like you are asking for is the ability to add simple inline functions without having to actually go somewhere else and write down a full function with its own name and whatnot. These are called lambdas.
If you are using C, I'd suggest just making small functions (perhaps even macros - ick) that build and return the type you want.
If you are using C++ there is some stuff in the standard libary in <algorithm> and <functional> you might be interested in. For your given example, I think find_if() would do what you are looking for. Generally that stuff is more of a PITA to use than it is worth though. You have to create a full-blown predicate object to do it, which is way more code and work than just creating your one-line function would have been in the first place.
Boost adds lambda support to C++, and the next standard is supposed to add it properly to the language.
Most functional languages support lambdas, but they generally don't use C syntax like this.
It will probably depend on the language you are writing your code. Generally the for loops do not return a value, unless you include them within an anonymous function also commonly known as lambda function.
In ruby you could accomplish something like that this way:
res = lambda {|array| for i in array do return true if i == 2 end }.call(0..4)
but in Java you will never be able to do such a thing easily without defining a new method.
Update
Generally speaking procedural methods (like ruby, perl, python, lisp, etc.) will provide you with built-in methods for handling anonymous functions, while other languages like C, C++, Java, etc. do not have these characteristics.
By the way, it should be clear that a for loop is a construct in all the languages and not a function, so it should never return a value (like an integer or a boolean or whatever else) but only handle the flow of the code through the processor. Anonymous functions provide us with the ability of incapsulating simple control codes in an inline function.
No, since they are both statements. You need an expression into the if condition. Furthermore, the return statement returns the function in which it has been used.
Why would you do that anyway?
In most languages, no.
for is a statement, not an operator. Unlike operators, statements do not yield a result and cannot be nested into expressions. The condition of the if statement expects an expression that can be evaluated to a boolean value, not a statement.
In languages like Perl and Python you may want to look at the map operator.
This is not good style. Split it up. If you are trying for one-liners Java is the wrong language my friend.