Skip to content

Deprecate Filename.chop_suffix in favor of check_and_chop_suffix.#1858

Closed
whitequark wants to merge 2 commits intoocaml:trunkfrom
whitequark:define-Filename.chop_suffix
Closed

Deprecate Filename.chop_suffix in favor of check_and_chop_suffix.#1858
whitequark wants to merge 2 commits intoocaml:trunkfrom
whitequark:define-Filename.chop_suffix

Conversation

@whitequark
Copy link
Copy Markdown
Member

@whitequark whitequark commented Jun 23, 2018

See https://caml.inria.fr/mantis/view.php?id=7812.

The defined behavior on erroneous input is to always raise Invalid_argument. This is chosen as opposed to e.g. doing nothing because it matches the de-facto behavior on earlier versions (which would be to raise a different exception) and avoids surprises when developing code for 4.08+ and running it on an earlier version. See the PR discussion for details.

end with the suffix [suff]. *)
the filename [name].

@since 4.08 If [name] does not end with the suffix [suff],
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If one wants to fully specify the behavior, one should explain what "ending with the suffix" means (also for other existing functions). This is not straightforward: for Windows ports, the comparison is case-insensitive on ASCII characters; but not, for instance on macOS (whose file-system is also usually case-insensitive, I believe).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've specified chop_suffix in terms of check_suffix. Your suggestion could be done as a separate patch if there is desire to do so.

@whitequark whitequark force-pushed the define-Filename.chop_suffix branch from 5a258a5 to 6c5369c Compare July 10, 2018 03:53
@alainfrisch
Copy link
Copy Markdown
Contributor

because it matches the de-facto behavior on earlier versions (which would be to raise a different exception)

What do you mean? Earlier versions fails only when the suffix is longer than the filename.

Since chop_suffix is likely to always be used in conjunction with check_suffix, I'm wondering whether it wouldn't be better to deprecate this function and expose:

  val chop_suffix_opt: string -> string -> string option

or even, using labels to avoid the confusion between the two string arguments:

  val chop_suffix_opt: suffix:string -> string -> string option

@whitequark
Copy link
Copy Markdown
Member Author

Earlier versions fails only when the suffix is longer than the filename.

Yes, that's what I meant--we should not change the behavior when the suffix is longer than the filename. If we changed chop_suffix to do nothing when the suffix is not present (such as when the suffix is longer than the filename), then code wirtten against 4.08 and ran against (e.g.) 4.07 would silently exhibit very surprising behavior.

Since chop_suffix is likely to always be used in conjunction with check_suffix, I'm wondering whether it wouldn't be better to deprecate this function and expose:

I have no opinion on this. I will implement whichever variant there is consensus for.

@damiendoligez
Copy link
Copy Markdown
Member

I would expect a name like check_and_chop_suffix or check_and_remove_suffix.

…7812)

Filename.chop_suffix is currently documented as having undefined
behavior on inputs that don't end with the suffix. The replacement
does not have this drawback.
@whitequark whitequark force-pushed the define-Filename.chop_suffix branch from 6c5369c to a758c65 Compare July 31, 2018 15:47
@whitequark whitequark changed the title Define Filename.chop_suffix n s if n does not end with s Deprecate Filename.chop_suffix in favor of check_and_chop_suffix. Jul 31, 2018
@whitequark
Copy link
Copy Markdown
Member Author

Updated.

@alainfrisch
Copy link
Copy Markdown
Contributor

alainfrisch commented Aug 29, 2018

  • Typo sufffix -> suffix.

  • The name of the function (check_...) suggests it is intended to be used on arbitrary inputs; if the suffix does not match, this is not a programmer error, and the function should thus not raise Invalid_argument.

  • The current direction in stdlib is to prefer functions returning options over raising exceptions, and it is a canonical use of named arguments to avoid confusion between arguments of the same type when there is no natural ordering between them. So I propose:

val check_and_chop_suffix : suffix:string -> string -> string option

Copy link
Copy Markdown
Contributor

@alainfrisch alainfrisch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See previous comments.

@nojb
Copy link
Copy Markdown
Contributor

nojb commented Oct 26, 2018

See also https://caml.inria.fr/mantis/view.php?id=7867 for a bug that could have been avoided with the option-returning function.

@alainfrisch
Copy link
Copy Markdown
Contributor

alainfrisch commented Oct 29, 2018

Superseded by #2125

EmileTrotignon pushed a commit to EmileTrotignon/ocaml that referenced this pull request Jan 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants