-
Notifications
You must be signed in to change notification settings - Fork 205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dart thinks a function returns num not int. #2741
Comments
Dart's There is language-level magic to help you with this problem. In the In the As for types accepting multiple types of values, that would be union types, #1222. Adding such is not on the list of features currently being worked on. |
This explains everything. Thank you for you time and clear explanation! |
For informational purposes. I've just been able to achieve the bare minimum i wanted (for now of course i do very profisonal code, i am just checking out available options), var abcdgcccc = funcfun; dynamic test_method(Types<int, String> string_or_int) { So later i will make sure, but see i got the very least i wanted, a FEELING of two types or multitypes type, I write this because someone might want to know he/she could do this this way. And if i could remove the ( ) paretheses string_or_int - it might be more than a 'FEELING'. See you. |
Update, i said about parentheses. I just didn't see such a simple solution like this:
Code analyser in Visual Studio Code doesn't seem to be offended by this. |
This comment was marked as duplicate.
This comment was marked as duplicate.
Not much tested i can improve it later. The closest and simplest i could get to union types, multitypes, twotypes (doubletypes) after much time of research and tests - as an example two types V means Value of a type, W means value of a type. abstract class Types<V, W> {
Types({V? v, W? w});
}
class TypesV<V, W> extends Types<V, W> {
V v;
TypesV(V this.v) : super(v: v);
}
class TypesW<V, W> extends Types<V, W> {
W v;
TypesW(W this.v) : super(w: v);
}
//And that's it. Out of it i guess you can make three-types (Types3<V, W, U>) or more specialized type versions.
String test_method(Types<int, String> string_or_int) {
return 'abc';
}
// examples how resilient it is in your Visual Studio Express or during compilation time errors.
Types<int, String> abcty = TypesV<int, String>(10);
Types<int, String> abcde = TypesW<int, String>('qwe');
var abcdek = TypesW<int, String>('qwe');
//error: Types<int, String> abc = TypesV<int, String>(10.4);
//var rtet = test_method(abc);
//error: Types<int, String> abca = TypesV<double, String>(10.4);
//error: Types abcdek = TypesW<int, String>('qwe');
//error:var abcdek = TypesW('qwe');
var rtetde = test_method(abcde);
var rtetdef = test_method(abcty);
var rtetdefp = test_method(abcdek);
It is not such a loophole but you definetely shouldn't use casting (see num and int as a result) like TypesV<num, String>(10) as Types<int, String>. It is not the point, you but normally don't do that, it is not the purpose ot this solution to do some strange casting of the Types class like object. |
Class hierarchies are a kind of union types. They can be used to implement explicit choices between values. Unordered structural union types would allow you to write A traditional implementation of a tagged union could be: abstract class Choice<A, B> {
factory Choice.a(A value) = ChoiceA;
factory Choice.b(B value) = ChoiceB;
ChoiceA<A>? asA() => null;
ChoiceB<B>? asB() => null;
}
abstract class ChoiceValue<T> {
T get value;
}
class ChoiceA<A> extends Choice<A, Never> implements ChoiceValue<A> {
final A value;
ChoiceA(this. value);
ChoiceA asA() => this;
}
class ChoiceB<B> extends Choice<Never, B> implements ChoiceValue<B> {
final B value;
ChoiceB(this. value);
ChoiceB asB() => this;
} |
Thanks Irhn, hope don't have a feeling that you have fallen into a trap of answering me :). Need some time to better process your anwer (my own solutions took me much time too and several attempts, i am slow thinking. The current unordered state of it all with unconsistent description is currently here https://github.com/brilliapps/multitypes-multivalues with the newest Mtypes class which is not a perfect solution but wanted to approach the topic completely, example: MTypes<MTypes<int, String>, MTypes<Future, List>> string_or_int_or_whatever - looks ugly, i know!) ). All these solutions to me seem to deliver the same key thing. We can create a function/method with an example param allowing one of the type, f.e. int or String and they are carried f.e. in a .value property of a passed-as-the-param object, and to me it's good, but could be great, if..., and all of this with errors in an editor before compilation. |
Let code speaks for itself
and then in main function:
Why there is error for "tretete" variable, while it returns int and has defined return type int
It seems to me there is issue whit dart - dart probably should know the return type is int.
By the way i need it working for StringOrInt pseudo "Type", which in reality is a simple function Type returning int or String when invoked.
But if it may return num not int like i mentioned before i have some trouble.
The types i define just like that and this is to tell you what type is accepted:
Would be nice if there were types accepting values of one of several types (like
FutureOr<T>
), or a self invoking function without parentheses( ) (abc = 10 + function_name_without_parentheses_invokation)
i could use f.e. in arythmetic operations like those in the code above.Also when i have a non int object i can write/override a "+" operator and do
int abc = my_non_int_object + 10;
- and it works. But i cannot change the order without error:int abc = 10 + my_non_int_object ;
- error - there is no operator for situation my_non_int_object to the right. Also related to these problems is that i cannot write two functions with the same name each, and one accepting int param and the second string. So not even one solution to two- or multi-types :)Is there any simple solution to these things? Is there something planned?
The text was updated successfully, but these errors were encountered: