Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple types for a single variable (parameter/return type)

Tags:

dart

I am very new to Dart so excuse me if I didnt see this part.

I want to make a union type e.g. for a function input. In TS this would be:

let variableInput: string | number

typedef doesnt really define types but functions and enums dont really help too.

On the other side how should it look like when a function return either one or the other of two types? There must be something I dont see here.

like image 324
Chrissi Grilus Avatar asked Apr 01 '20 10:04

Chrissi Grilus


People also ask

Can a function have 2 return types?

No, you don't have two return types. It's a generic method you are seeing.

Can a variable have multiple types?

How can a variable have 2 data types? It cannot. An object has one type. A single type can represent values of different types (unions, std::variant ).

Can a function return multiple types Python?

You can return multiple values from a function using either a dictionary, a tuple, or a list. These data types all let you store multiple values.

How do I return two different data types in TypeScript?

To allow a function to return multiple data types, you simply add a pipe after a data type. Related articles: I like to tweet about TypeScript and post helpful code snippets. Follow me there if you would like some too!

How to define a function with multiple return types in typescript?

Use a union type to define a function with multiple return types in TypeScript, e.g. function getValue (num: number): string | number {}. The function must return a value that is represented in the union type, otherwise the type checker throws an error. Copied! We used a union type to set the return type of a function with multiple return types.

Can a generic class have multiple type parameters in Java?

A Generic class can have muliple type parameters. Following example will showcase above mentioned concept. Create the following java program using any editor of your choice. GenericsTester.java

How to return multiple values from array in Java?

We can use following solutions to return multiple values. We can return an array in Java. Below is a Java program to demonstrate the same. We can use Pair in Java to return two values. We can encapsulate all returned types into a class and then return an object of that class. Let us have a look at the following code.

How to return two values from a pair in Java?

If returned elements are of different types. Using Pair (If there are only two returned values) We can use Pair in Java to return two values. import javafx.util.Pair; class GfG {. public static Pair<Integer, String> getTwo () {. return new Pair<Integer, String> (10, "GeeksforGeeks"); }


Video Answer


1 Answers

There are no union types in Dart.

The way to do this in Dart is returning/accepting dynamic as a type:

dynamic stringOrNumber() { ... }

void main() {
  final value = stringOrNumber();

  if (value is String) {
    // Handle a string value.
  } else if (value is num) {
    // Handle a number.
  } else {
    throw ArgumentError.value(value);
  }
}

See also: https://dart.dev/guides/language/sound-dart

like image 61
creativecreatorormaybenot Avatar answered Jan 03 '23 18:01

creativecreatorormaybenot