Why is `toIdString` member of `Entity`? - xodus

My question is quite simple: Why is the method toIdString member of jetbrains.exodus.entitystore.Entity and not member of jetbrains.exodus.entitystore.EntityId?

Entity#toIdString() calls EntityId#toString(). For an Entity e, it's just a way to write e.toIdString() instead of e.getId().toString().

Related

How can I map controller endpoints to actions on F#?

I am probably missing something here as I am new to F#, however, I need the following:
open Microsoft.AspNetCore.Mvc
[<ApiController>]
[<Route("[controller]")>]
type MyController () =
inherit ControllerBase()
//[<HttpGet(Name = "Ip")>] doesn't work neither.
[<HttpGet>]
[<Route("[controller]/[action]")>]
member _.Ip() =
"192.168.199.2"
The URL: https://localhost:5001/my/ip should return: 192.168.199.2.
The error message I am getting instead:
{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"00-389e8d2f6bc3a342a3754b5c5ce7915f-7e6e851c78f47c4f-00","errors":{"id":["The value 'ip' is not valid."]}}
I don't have much experience with ASP.NET Core, but I think the problem is that you have a route set at both the class and member level. These are additive, so the actual URL of your Ip action is currently https://localhost:5001/my/my/ip.
To fix this, remove the Route attribute entirely from the class level, or remove the [controller] prefix from your member-level route:
[<ApiController>]
[<Route("[controller]")>] // controller is specified here, so...
type MyController() =
inherit ControllerBase()
[<HttpGet>]
[<Route("[action]")>] // ...no controller specified here
member _.Ip() =
"192.168.199.2"

Fable: how to override a JS class method imported from a JavaScript external library? (typed)

What is the expected way to extend a class imported from a Javacript library while at the same being capable to call parent's members?
I tried several alternatives, using abstract classes apparently worked without errors but the child cannot call parent's abstract method, with interfaces there were no errors either but I cannot call to parent's class as there is no reference.
The best method I found is the following one overriding the mezhods, and althought the yielt code works, the compiler still emits an error:
error FSHARP: No abstract or interface member was found that corresponds to this override (code 855)
My current code:
[<Import("DataManager", from="library/data")>]
type DataManager<'Model> (conf:obj) =
class
member this.insert(_:'Model):Promise<obj> = jsNative
member this.update (_:'Model):Promise<obj> = jsNative
end
type MyAdaptor<'Model> (conf, inst)=
inherit DataManager<'Model> (conf)
let DB:obj = inst
do
printf "I've been created"
override this.insert(o:'Model):Promise<obj> =
printf "insert method comes with object:"
console.log o
base.insert o
//Constructors.Promise.Create o
override this.update(o:'Model): Promise<obj> =
printf "Update method comes with object:"
console.log o
base.update o
//Constructors.Promise.Create o
Previously I also tried to use just members and still call base's method but althought it compiled without issue, when calling instance's methods only parent's code was executed. I am afraid it might be a bug.
I also had several tries callint the inheritance manually in a self-made constructor but it usually fails to compile because the imported JS was either not recognize as a valid constructor or then I couldn't include the method definitions (I care about type safety).
It turns out that in F# people cannot override a method which has not been declared previously as abstract.
For that the solution was to declare the methods as abstract and provide a default implementation before override.
[<Import("DataManager", from="library/data")>]
type DataManager<'Model> (conf:obj) =
class
abstract member insert: 'Model -> Promise<obj>
default this.insert(_:'Model):Promise<obj> = jsNative
abstract member update:'Model -> Promise<obj>
default this.update (_:'Model):Promise<obj> = jsNative
end
After that it is possible to override the child class without issues.

F# make member value based on member function

I have made a member function in a class. Afterwards I want to make a member value that is set to the result of this member function.
type MyType() =
member this.drawFilledPlanet(xCoord:int, yCoord:int, pWidth:int, pHeight:int, color) =
let brush = new System.Drawing.SolidBrush(color)
this.window.Paint.Add(fun e ->
e.Graphics.FillEllipse(brush, xCoord, yCoord, pWidth, pHeight))
member val theSun = drawFilledPlanet(350,350,100,100, this.yellow)
I am getting the error that drawFilledPlanet is not defined.
Can someone tell me what is up?
Because drawFilledPlanet is a member function, it needs a class instance on which it's to be called. If you're calling it from another member function, you would use that member's definition to name the current instance:
member this.f() = this.drawFilledPlanet ...
In your case, however, since you're defining a member val, you don't have that opportunity. In this situation, you can name the current instance at the very top of the class declaration:
type MyType() as this =
...
member val theSun = this.drawFilledPlanet ...
One thing I'd like to point out is that this definition may not have the effect that you expect. If you define theSun this way, the drawFilledPlanet method will only get executed once at class initialization, not every time theSun is accessed. Did you intend that? If no, then you need to change the definition. If yes, then why do you need this definition at all?

Defining member as event handler in F#

This is probably asked several times but I just can't find an example.
My goal is to define an event handler for an event and the handler should be a member of the class. In other words I don't want to use function since I need to access instance variables and members
The latest variation I've tried:
namespace A
type ValueList<'TValueItem when 'TValueItem :> IValueItem>() =
inherit System.Collections.ObjectModel.ObservableCollection<'TValueItem>()
// This is causing error: The value or constructor 'ValueList_CollectionChanged' is not defined
let collectionChangedHandler = new System.Collections.Specialized.NotifyCollectionChangedEventHandler(ValueList_CollectionChanged)
// Constructor code
do base.CollectionChanged.AddHandler(collectionChangedHandler)
// Handles collection changed events for data items
member this.ValueList_CollectionChanged(sender : obj, e : System.Collections.Specialized.NotifyCollectionChangedEventArgs) =
// The code I want to run goes here
...
Or is this maybe a completely wrong approach?
Looks like you're looking for the self-identifier syntax:
type ValueList<'TValueItem when 'TValueItem :> IValueItem>() as this =
The as this (or any other identifier in place of this) allows to refer to the instance being constructed from the constructor.
You could then change your other lines to use the identifier:
let collectionChangedHandler = new System.Collections.Specialized.NotifyCollectionChangedEventHandler(this.ValueList_CollectionChanged)
do this.CollectionChanged.AddHandler(collectionChangedHandler)
For this to be valid as-is, the ValueList_CollectionChanged method also needs to be in curried form:
member this.ValueList_CollectionChanged (sender : obj) (e : System.Collections.Specialized.NotifyCollectionChangedEventArgs) =
As an alternative to using curried arguments, you can use a lambda to transform the arguments where the handler is instantiated, e.g. .NotifyCollectionChangedEventHandler(fun sender e -> this.(...).

f# function not recognized as parameter

Please unwrap these type signatures to help me understand why this doesn't work.
Then, if you have a solution, that would be great too.
I have this code and the agent.Post command has the signature Observer.Create<'T>(onNext: Action<'T>) : IObserver<'T>
let reservatinoRequestObserver = Observer.Create agent.Post
interface IHttpControllerActivator with
To my knowledge, this means that Observer.Create should take an Action with a single generic parameter and then return an IObserver.
Now the definition of Post is member MailboxProcessor.Post : message:'Msg ->unit
So... Post is a method, no? It is a method that takes a single parameter no? And it returns void no? So shouldn't it be a candidate for Observer.Create? Isn't that the exact specification of Action<'T>?
Well, somethings up, I get This function takes too many arguments, or is used in a context where a function is not expected:
Help me out... I freely admit I suck at F#
First, agent.Post returns unit, which is a different thing from void. F# will usually convert back and forth between void and unit for you, but they are not the same thing.
Second, F# functions do not implicitly convert to .NET delegates.
But there are some ways to do it:
You can explicitly create the delegate using its constructor:
let o = Observer.Create (new Action<_>( agent.Post ))
Lambdas are nicely wrapped too
let o = Observer.Create (fun msg -> agent.Post msg)
Use F# Rx-wrappers
Also there are a couple of F# wrappers/interop for Rx on nuget - just have a look, I think any will do

Resources