Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a F# class with a sequence property

Tags:

f#

Here's the code:

type Advertisement() =
    member val Photos = seq<images> with get,set

but this returns a function signature for the property Photos as

seq<images> -> seq<images>

I just want the property to represent a sequence of images

seq<images>

What am I missing?

like image 319
user1206480 Avatar asked Sep 14 '16 09:09

user1206480


People also ask

What does %f do in Python?

F-strings provide a way to embed expressions inside string literals, using a minimal syntax. It should be noted that an f-string is really an expression evaluated at run time, not a constant value. In Python source code, an f-string is a literal string, prefixed with 'f', which contains expressions inside braces.

How do you write an F-string?

Strings in Python are usually enclosed within double quotes ( "" ) or single quotes ( '' ). To create f-strings, you only need to add an f or an F before the opening quotes of your string. For example, "This" is a string whereas f"This" is an f-String.

What is an F-string?

Also called “formatted string literals,” f-strings are string literals that have an f at the beginning and curly braces containing expressions that will be replaced with their values. The expressions are evaluated at runtime and then formatted using the __format__ protocol.


2 Answers

The issue here is that you want to use seq<images> as a type annotation, but you're using it as an expression. These have different meanings; also, it's not clear what sequence you want to assign to Photos.

For example, if you want to initialize Photos to an empty sequence, you could use Seq.empty and have seq<images> as a type annotation:

    member val Photos = Seq.empty : seq<images> with get, set

Or, as ildjarn suggested, use an explicit type parameter for Seq.empty<'T>:

    member val Photos = Seq.empty<images> with get, set

If the type of Photos is clear from its usage in the same code file, the compiler will infer the type and you can even shorten it to:

    member val Photos = Seq.empty with get, set

Why did the original code return a fuction?

When you wrote seq<images> where an expression, not a type is expected (i.e. not behind a : or as a type parameter), the compiler resolved it to Operators.seq<'T>, which is the function of signature seq<images> -> seq<images> you are getting.

The implementation of seq is a little weird; it is supposed to behave like a computation expression but is actually a special case for the compiler. It is used both to cast to seq<'T> via the function you've seen, and to signify sequence expressions, as in seq { yield 1 }.

like image 160
Vandroiy Avatar answered Oct 12 '22 02:10

Vandroiy


seq<images> is referring to the seq builder, like the one that you use in the following way

let foo = seq { yield "bar" }

You can actually see this if you hit F12 on seq. To instantiate an empty sequence you need to use Seq.empty<images>.

like image 26
Levi Botelho Avatar answered Oct 12 '22 02:10

Levi Botelho