Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a cast function for CmdParam binding #146

Closed
wants to merge 1 commit into from

Conversation

TysonMN
Copy link
Member

@TysonMN TysonMN commented Oct 8, 2019

Adds a cast function to CmdParam so that the parameter can be of type 'param instead of type obj.

@TysonMN TysonMN mentioned this pull request Oct 8, 2019
@cmeeren
Copy link
Member

cmeeren commented Oct 8, 2019

Isn't this a breaking change?

Could you give some usage examples? I fail to see how this is an improvement over simply

param |> unbox<'param> |>

in the function.

@TysonMN
Copy link
Member Author

TysonMN commented Oct 8, 2019

Yes, it is a breaking change. I created it to aid the discussion of #144. In general, I think this is an improvement (since otherwise both exec and canExec would generally contain duplicate casting code). However, the primary reason I wrote it though was to cite it from #144 (comment).

@cmeeren
Copy link
Member

cmeeren commented Oct 8, 2019

Thank you for bringing this to my attention! I agree that the typing can be improved. However, I think there's a better way to do it using type parameters (written from the top of my head):

static member cmdParam<'param>
    (exec: 'param -> 'model -> 'msg,
     ?wrapDispatch: Dispatch<'msg> -> Dispatch<'msg>)
    : string -> Binding<'model, 'msg> =
  CmdParamData {
    Exec = fun p model -> exec (unbox<'param> p) model |> ValueSome
    CanExec = fun _ _ -> true
    AutoRequery = false
    WrapDispatch = defaultArg wrapDispatch id
  } |> createBinding

Usage:

"Foo" |> Binding.cmdParam<int>(fun p m -> SetInt p)

Or, in fact, since F# can infer it in this case (assuming SetInt is int -> ...) and probably most other cases:

"Foo" |> Binding.cmdParam(fun p m -> SetInt p)

I don't even think this is a breaking change, because 'param will likely be inferred to be obj for existing usages using unbox. Careful thought is needed to ascertain whether this will hold generally, though.

What do you think?

@TysonMN
Copy link
Member Author

TysonMN commented Oct 26, 2019

From the perspective of the code in this PR, you are (in part) suggesting that we hard code cast: obj -> 'param to unbox<'param>.

I see trouble with that. Looking at the UiBoundCmdParam sample, it seems like the parameter can be null:

"Command" |> Binding.cmdParamIf(
(fun p m -> Command),
(fun p m -> not (isNull p) && p :?> int <= m.EnabledMaxLimit),
true)

Calling (say) unbox<int> null throws a NullReferenceException.

Maybe if hard coding cast to Option.ofObj |> Option.map unbox<'param> would work? I tried and got confusing compiler errors.

Honestly though, I don't care about making this change. I vote we abandon this PR. We can create an issue about this change if it is important enough.

@cmeeren
Copy link
Member

cmeeren commented Oct 26, 2019

Honestly though, I don't care about making this change. I vote we abandon this PR. We can create an issue about this change if it is important enough.

Let's leave it as it is, then. My guess is that cmdParam and cmdParamIf aren't used nearly as much as cmd and cmdIf. If anyone cares enough about this and comes up with a good way to improve the API, I'm open to suggestions in a new issue.

@cmeeren cmeeren closed this Oct 26, 2019
@TysonMN TysonMN deleted the extract_cmd_param_casting branch October 26, 2019 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants