Ambiguous match found when calling String.Split() - ambiguous

I'm programming in JScript.NET which is similar to C# . I want to split a string on multiple characters, in this case " - ".
The problem is when I do that like this (which should be the way to do it according to this thread):
var text = "test - test2";
var array = [" - "];
var val = text.Split(array, StringSplitOptions.None);
I get "Ambiguous match found". This is because the String class has both a Split(Char[], StringSplitOptions) and a Split(String[], StringSplitOptions) function, and the compiler doesn't know which one to use.
So my question is then. How do I tell the compiler that I'm using a string array when the arrays in JScript.NET are dynamically typed?
Edit: As far as I know, JScript.NET use the same APIs as C#. So this is the String class I'm using. However, I think the syntax is the same as JavaScript. Maybe someone could confirm this?
Edit2: So if there is a way to enforce a type in JScript.NET so the compiler knows which type is used, I guess that would be the answer for my case as well? JScript.NET does not have the same syntax as C#.

I figured it out once I realized I was coding in JScript.NET and not JScript, which led me to a bunch of useful guides. One of them specifically mentioned how to create typed arrays.
It turns out it was a easy as this:
var array : String[] = [" -"];

Related

Are Symbols deprecated in Dart?

From reading this (https://www.educative.io/edpresso/what-is-a-dart-symbol) it seems that Symbols are deprecated in Dart. But I've had trouble finding an explanation for why to avoid them. This answer (Dart symbol literals) gives some explanation of what symbols are in dart, certain cases when they're commonly used. And it suggests "you shouldn't need to use symbols outside of those cases." Shouldn't need is different than shouldn't. Are they deprecated or not? If they are deprecated, why?
The docs here: (https://api.dart.dev/stable/2.12.4/dart-core/Symbol-class.html) don't mention deprecation. I couldn't find any mention of Symbols here: (https://dart.dev/guides/language/effective-dart) either for or against.
In this example the symbol seems to be working as I'd like it to:
class TrafficLight {
Symbol color;
TrafficLight(this.color);
}
void main() {
var t = TrafficLight(#red);
print(t.color == #red);
print(t.color == #green);
}
//returns:
// > true
// > false
If I have thousands of traffic lights, thousands of identical Strings are a waste of RAM. What's the common / best practice way of handling these type of situations in Dart? Enums? Creating a TrafficLightColor class?
Thanks in advance for any light you can shed on this issue.
Response to comments
#julemand101
void main() {
String a = '123';
String b = a;
a += 'foo';
print('a: $a');
print('b: $b');
}
return is:
a: 123foo b: 123
If they were pointing to the same object, I'd expect return to be
a: 123foo b: 123foo
#julemand101, You're absolutely right, my thinking was off. Thanks for the explanation.
Symbols are not deprecated. They are mainly used for reflection-like functionality like dart:mirrors, Object.noSuchMethod (the memberName and namedArguments names) and Function.apply (again the named arguments names). If you don't need those, you likely don't need to bother with symbols. You can, they're just objects, but not particularly useful objects.
(Some libraries use private symbols, like #_foo to create a library-private sentinel object, but you could also just do final _mySentinel = Object();.)
The best way to handle the traffic lights situation is enums.
You could use symbol literals (#red, #green) or string literals ("red", "green") or magic numbers (1, 2), but the only approach to creating a fixed set of values to represent a specific thing that is language and type-system supported is enums.
You can make your own enum-like class if you want to, and it'll be just as good as enums, except that you won't get a warning if you forget a case in a switch.
(If you do use string literals, you'll likely find that they are canonicalized too, so the string data won't take up more space whether they occur once or thousands of times).

What does the "as" keyword do in Dart language?

I'm confused as to the uses of "as" keyword.
Is it a cast operator or alias operator?
I encountered the following code on the internet which looked like a cast operator:
var list = json['images'] as List;
What does this mean?
as means different things in different contexts.
It's primarily used as a type cast operator. From the Dart Language Tour:
as: Typecast (also used to specify library prefixes)
It links to an explanation of how as is also used to add a prefix to an imported library to avoid name collisions. (as was reused to do different things to avoid needing extra keywords.)
just to add the as keyword is now flagged by the linter and they prefer you to use a check like is
if (pm is Person)
pm.firstName = 'Seth';
you can read more here https://github.com/dart-lang/linter/issues/145
As the language tour says:
Use the as operator to cast an object to a particular type if and only if you are sure that the object is of that type.
Following with your example:
var list = json['images'] as List;
You would use as here to cast or convert json['images'] into a <List> object.
From another SO post (talking about explicit cast vs. as):
as ... is more like an assertion, if the values type doesn't match as causes a runtime exception.
You can also use it when importing packages. A common example is the dart:convert as JSON which then can be reached final foo = JSON.jsonDecode(baz)
It's casting, your code is similar as:
List list = json['images'];

Accessing an explicit conversion operator from F#

I am getting the following error when reading date values out of postgresql using npgsql:
This expression was expected to have type
DateTime
but here has type
NpgsqlTypes.NpgsqlDate
Now the npgsql docs refer to an explicit operator being defined:
[C#]
public static explicit operator DateTime(
NpgsqlDate date
);
but I can't figure out how to access this from F#.
There are several kludgy, longhand ways of achieving what I need, but I am disappointed and frustrated that I was unable to find a way of accessing the inbuilt cast.
I tried the old Convert.ToDateTime(...), but even that doesn't work.
Anybody got a clue? Thx.
The explicit conversion operator can be accessed by calling the op_Explicit (I had the casing wrong in the earlier comment; I had not tried it myself then) function on the type:
let date = NpgsqlDate.op_Explicit npgsqlDate
I have also found various places (like in Yan Cui's blog here) that define an F# operator like !> for (all!) explicit conversions for convenience, so you can just say
let date = !> npgsqlDate
I think that is a pretty neat way.

What is the syntax for implicit cast operator in dart?

I would like to cast instances of my custom class A to int. What is the syntax of the implicit cast operator? (I thought I remembered that there is such a feature but I can't find it on the web)
int a = (new A());
You can also use as to help tell the tools "no, really, treat this object as this type".
A good example of this is when you have to deal with dart:html's querySelector() function.
FormElement form = querySelector('#sign-up') as FormElement;
In the above, the object returned by querySelector('#sign-up') is checked that it is really an instance of FormElement.
Learn more at https://www.dartlang.org/docs/dart-up-and-running/ch02.html#operators
Type annotations are not allowed to affect behavior in Dart. If you're not running in checked mode, then this:
int a = new A();
will work the same as this:
var a = new A();
at run-time. In other words, when not in checked mode, you're welcome to store your A in a variable annotated as an int, but no actual conversion takes place.
If you are running in checked mode, the first form will give you a runtime exception.
I'm not sure, but I think what you're asking for is a way to define a conversion between your class A and int that will happen automatically when "cast" to an int. No such thing exists, to my knowledge. You should simply define a method to do so. For example:
int a = new A().to_i();

Global constants in F# - how to

I need to set a version number to be used in the AssemblyVersion attribute by several related projects.
In C# I use the following
public class Constants {
public const string Version = "1.2.3.4";
}
then it can be used as follows:
[assembly:AssemblyVersion(Constants.Version)]
What would be the equivalent construct in F#. All my attempts to come up with a binding which can be accepted as an attribute argument did not work.
Use the attribute Literal:
[<Literal>]
let version = "1.2.3.4"
[<assembly:AssemblyVersion(version)>]
Since I stepped into this trap myself I thought I'd share for anyone following.
A 'Literal' requires that the letter starts with a capital letter. This will hit you when you try to use the literal in a pattern matching construct.
Reference:
Literal attribute not working

Resources