How to take parameters from the command line inside the main function in Haxe? [duplicate] - haxe

This question already has an answer here:
Haxe - Print command line arguments
(1 answer)
Closed 2 years ago.
I'm new to Haxe. My school tasked me to write a Jack compiler in Haxe, so I started learning the language. I can't find anywhere how to take parameters inside the main function. That is, I'd like to call my executable like this :
./Main.exe arg1 arg2 arg3
...and read these args inside the main function.

See: https://api.haxe.org/Sys.html#args.
This method returns your passed arguments as Array<String>.
static function main() {
var args = Sys.args();
var arg1 = args[0];
var args = args[1];
//...
}

Related

What is the idiomatic Rust way to deal with a lot of if-let-else code? [duplicate]

This question already has answers here:
How can I reduce the verbosity of matching every call that returns a Result or Option?
(1 answer)
How to cleanly get the path to the bash history?
(1 answer)
Closed 5 years ago.
I am trying to find out the current home directory of the user (toy project, learning Rust) and this is the code I am at currently:
let mut home_dir = if let Some(path) = env::home_dir() {
if let Some(path_str) = path.to_str() {
path_str.to_owned()
} else {
panic!("Found a home directory, but the encoding of the filename was invalid")
}
} else {
panic!("Could not find home directory for current user, specify prefix manually");
};
I come from a heavily managed-language background so maybe that is why this appears to me as a bit of an anti-pattern, but I am trying to understand if I there is a better way to do this.
In Kotlin I would deal with this situation using:
val homeDir = env.getHomeDir()?.toString() ?: throw RuntimeException(...)
The way the ? macro works in Rust requires me to use a function that returns a Result which I believe main() does not, and it doesn't necessarily make sense anyways. Any pointers?

Why doesn't this GHCi expression have any output? [duplicate]

This question already has answers here:
GHCi "let" -- what does it do?
(4 answers)
Closed 6 years ago.
Edit: Okay, I know nothing is wrong with this, but I don't know why it isn't giving output
let y = 2 * x where x = sum[1..3]
I only wonder, because this other expression DOES give output
let x = sum[1..3] in 2 * x
Ah. So let ... in ... is an expression. However, let ... can also occur in a do block. I encourage you to think of GHCi's behaviour as follows: if you enter something that just looks like a raw expression, it evaluates it and prints the result. On the other hand, if what you entered looks like it might belong in an IO do block, it will simply execute that action.
- #Alec

Pass arguments to function by string replacement [duplicate]

This question already has answers here:
How to pass / evaluate function arguments within another function for use with ggplot?
(1 answer)
pass character strings to ggplot2 within a function
(2 answers)
Closed 9 years ago.
How do you wade through the eval(), parse() and other-function swamp? This should be straightforward, thus I omit the data.
Original code, with attributes in the data set and a workaround for the chart title.
require(ggplot2)
ggplot(data = qs) + geom_bar(aes(x = G74_Q0005b)) +
ggtitle(attr(qs, "variable.labels")[grep("G74_Q0005b", names(qs))])
Here is a function that simply passes the variable name:
plot.label <- function(var){
ggplot(data = qs) + geom_bar(aes(x = var)) +
ggtitle(attr(qs, "variable.labels")[grep(var, names(qs))])
}
But obviously var alone is not enough and I am no programmer.
Possibly related?
How to pass / evaluate function arguments within another function for use with ggplot?
passing parameters to ggplot
You need to use aes_string for the aesthetics part. This takes a variable containing a string as an argument.
plot.label <- function(var){
ggplot(data = qs) + geom_bar(aes_string(x = var)) +
ggtitle(attr(qs, "variable.labels")[grep(var, names(qs))])
}

Basic OCaml function returns type error

I've been trying to run this function :
let insert_char s c =
let z = String.create(String.length(s)*2 -1) in
for i = 0 to String.length(s) - 1 do
z.[2*i] <- s.[i];
z.[2*i+1] <- c;
done;
z;;
print_string(insert_char("hello", 'x'));;
However the interpreter returns a type error at the last line "type is string * char" and it expected it to be string. I thought my function insert_char created a string. I don't really understand, thanks.
You define your function as a curried function, but you're calling it with a pair of values. You should call it like this:
insert_char "hello" 'x'
OCaml doesn't require parentheses for a function call. When two values are placed next to each other with nothing between, this is a function call.
The syntax of function application in OCaml is f arg1 arg2, not f(arg1, arg2). So it would be print_string (insert_char "hello" 'x').
(Once you fix that you'll discover that your code has other problems, but that is unrelated to your question.)

What is the difference between String and string and var? [duplicate]

This question already has answers here:
What is the difference between String and string in C#?
(66 answers)
Closed 9 years ago.
I have this use case:
using System;
namespace ConsoleApplication11
{
using System.Collections.ObjectModel;
public class Program
{
static void Main(string[] args)
{
var test2 = "Name";
string test1 = "Name";
String test3 = "Name";
Console.WriteLine(test1 + test2 + test3 + "NameNotDefined");
}
}
}
What will do the compiler:
in this line Console.WriteLine(test1 + test2 + test3 + "Alugili");?
with the last one "NameNotDefined"?
Did the compiler call ToString() to each of them and than apply the + Operator?
Any body can please explain to me the difference between the var and String and string and "" and how the complier will interact with them?
All three compile identically in C#. The "var" keyword is used to infer the type at compile-time, and the compiler infers that you're using a string (which is the same thing as String).
string is an alias for System.String, both of them compile to the same code, so at execution time there is no difference.
The + is used for string concatenation, so when you write test1 + test2 + test3 + "Alugili" it concatenates all of the strings and form one string. In .net string's are immutable i.e. strings cannot be altered. When you alter a string, you are actually creating a new string.
About var MSDN says this :
Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit type var. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type. The following two declarations of i are functionally equivalent:

Resources