F# Weekly #33, 2014

Welcome to F# Weekly,

A roundup of F# content from this past week:

News

Core and Lang News

Videos/Presentations/Courses

Blogs

That’s all for now.  Have a great week.

Previous F# Weekly edition – #32Subscribe

 

F# Weekly #32, 2014

Welcome to F# Weekly,

A roundup of F# content from this past week:

News

Core and Lang News

Videos/Presentations

Blogs

That’s all for now.  Have a great week.

Previous F# Weekly edition – #31Subscribe

F# Weekly #31, 2014

Welcome to F# Weekly,

A roundup of F# content from this past week:

News

Core and Lang News

Videos/Presentations

Blogs

That’s all for now.  Have a great week.

Previous F# Weekly edition – #30Subscribe

F# Weekly #30, 2014

Welcome to F# Weekly,

A roundup of F# content from this past week:

News

Core and Lang News

Videos/Presentations

Blogs

That’s all for now.  Have a great week.

Previous F# Weekly edition – #29Subscribe

F# Kung Fu #4: Avoid using relative paths in #r directives

Today, Vladimir Makarov faced with quite interesting ‘bug'(very unexpected behavior) of FSI. The initial goal was quite simple – count number of NuGet packages, which have “ASP.NET” in title. As a result, there was created a script that perfectly works in compiled form and crashes in FSI, here it is:

#r "../packages/nuget.core.2.8.2/lib/net40-Client/Nuget.Core.dll"
#r "System.Xml.Linq.dll"

let repository =
    NuGet.PackageRepositoryFactory.Default.CreateRepository
        "https://nuget.org/api/v2"

let aspnet = query {
    for p in repository.GetPackages() do
    where (p.Title.Contains "ASP.NET")
    count
}

When we run this in FSI we got an exception

System.ArgumentException: Incorrect instance type
Parameter name: obj
at Microsoft.FSharp.Quotations.PatternsModule.mkInstanceMethodCall(FSharpExpr obj, MethodInfo minfo, FSharpList`1 args)
at Microsoft.FSharp.Quotations.ExprShapeModule.RebuildShapeCombination(Object shape, FSharpList`1 arguments)
at Microsoft.FSharp.Primitives.Basics.List.map[T,TResult](FSharpFunc`2 mapping, FSharpList`1 x)
at Microsoft.FSharp.Linq.QueryModule.walk@933-1[a](FSharpFunc`2 f, FSharpExpr p)
at Microsoft.FSharp.Linq.QueryModule.EvalNonNestedInner(CanEliminate canElim, FSharpExpr queryProducingSequence)
at Microsoft.FSharp.Linq.QueryModule.EvalNonNestedOuter(CanEliminate canElim, FSharpExpr tm)
at Microsoft.FSharp.Linq.QueryModule.clo@1741-1.Microsoft-FSharp-Linq-ForwardDeclarations-IQueryMethods-Execute[a,b](FSharpExpr`1 )
at <StartupCode$FSI_0002>.$FSI_0002.main@() in D:\Downloads\test.fsx:line 4

When we looked more carefully to FSI output, we saw that:

nuget.core

WAT? FSI lies to us?! First message says that correct version of DLL was referenced, but then FSI loads completely wrong old version installed with ASP.NET. Why? Let’s check what #r actually does…

#r means to reference by dll-path; focusing on name. This means that FSI will use the file name first, looking in the system-wide search path and only then try to use the string after #r as a directory-relative hint

So that means, #r is not reliable way to reference assemblies. You can get into the situation when your script depends on the environment: assemblies in GAC, installed software (like version of ASP.NET) and so on. To avoid this it is better to explicitly specify an assembly search path (#I) and then reference the assembly:

#I "../packages/nuget.core.2.8.2/lib/net40-Client"
#r "Nuget.Core.dll"
#r "System.Xml.Linq.dll"

let repository =
    NuGet.PackageRepositoryFactory.Default.CreateRepository
        "https://nuget.org/api/v2"

let aspnet = query {
    for p in repository.GetPackages() do
    where (p.Title.Contains "ASP.NET")
    count
}

Thanks to Vladimir Makarov for interesting challenge and be careful in your scripts.

Type Providers, Record / Union Types and Constant Type unsupported

Originally posted on FourEightThree:

When writing type providers you are required to define code that will run at run-time with a quotation. This in itself is not a problem, however if you try and pass a none native type to the quotation you will receive the following error,

Unsupported constant type: xxxx

There is a stack-overflow post here which has an example and a good explanation of the reasons why. A typical work around is to use each field from a record and pass it to a function call in the quotation as an array or as individual parameters. Either way this can end up being quite painful.

So how can we work around this. Well, what we need to do is build a new instance of the object we are trying to pass to the quotation within the quotation itself, and then use the variable that holds this new instance as the parameter…

View original 268 more words

F# Weekly #29, 2014

Welcome to F# Weekly,

A roundup of F# content from this past week:

News

Core and Lang News

Videos/Presentations

Blogs

That’s all for now.  Have a great week.

Previous F# Weekly edition – #28Subscribe

Guess a project by logo

The set of F#-branded logos (projects) with the sharp ‘#’ sign in the top-left corner ;)
Please let me know if I missed something.

F# Weekly #28, 2014

Welcome to F# Weekly,

A roundup of F# content from this past week:

News

Core and Lang News

Video/Presentations

Blogs

That’s all for now.  Have a great week.

Previous F# Weekly edition – #27Subscribe

Follow

Get every new post delivered to your Inbox.

Join 108 other followers