How can I get a function that returns different types in F #?

I made a scanner in F #. Currently, it returns a list of tuple sets with type (token, string).

Ideally, I would like to return a list of tuples that can contain different types. For instance:

(Token, string) 
//if it an identifier

(Token, float)
//if it a float. 

(Token, int)
//etc

So, basically, I would like to return a type (Token, _), but I'm not sure how to indicate this. Now he just has errors complaining about inconsistent types. I look through my book and Wikibooks, but I'm not quite sure what this is called.

If this is really not possible, I think I can convert the types later, but I was hoping I could just return the material this way.

+3
source share
3 answers

F #. :

type Expression =
| Identifier of string
| FloatValue of float
| IntValue of int
| ...

, (Token * Expression) list. - , (Token * obj) list. , .

+12

, , kvb, - . , F #, Token, , , :

type Token = 
// Some toknes with no additional information
| LParen | RParen | Begin | End
// Some tokens with additional values
| Identifier of string
| IntValue of int
| ...

, Token list.

+4

, ( , Choice1of3 ..).

+1

Source: https://habr.com/ru/post/1791870/


All Articles