Open vxern opened 5 months ago
Hello! What's the use case here? Could you give some examples from your programs and how these functions would help with them. Thanks!
@lpil, this appears to be merely for convenience. See some examples in #619.
I don't know how prevalent this pattern is throughout the various gleam libraries, but at least one precedent for this type of "builder" pattern can be seen in the gleam http library, where there are a number of "setter" functions for the Request
type, such as set_header
, set_body
, set_scheme
, set_host
, etc.
Sure.
https://github.com/vxern/tatoeba/blob/main/src%2Ftatoeba%2Fapi.gleam#L11
This is an example where it would have been quicker and nicer to write:
pub fn url() -> String {
url.new()
|> url.set_scheme("https")
|> url.set_host("tatoeba.org")
|> url.set_path("/eng/api_v0")
}
over:
pub const url = Uri(
scheme: Some("https"),
userinfo: None,
host: Some("tatoeba.org"),
port: None,
query: None,
path: "/eng/api_v0",
fragment: None,
)
They seem about the same to me, and it's nice that the constant one has no runtime cost.
I've done a search on GitHub and it seems that directly constructing Uris is uncommon. It doesn't seem like something that there's a huge benefit to adding a small amount of convenience for.
Considering the signature of
Uri
, to make working with the type a little nicer, I would propose to add value setter primitives corresponding to fields on theUri
type:The primitives would have the following signatures:
set_scheme(uri: Uri, scheme: String) -> Uri
set_userinfo(uri: Uri, userinfo: String) -> Uri
set_host(uri: Uri, host: String) -> Uri
set_port(uri: Uri, port: Int) -> Uri
set_path(uri: Uri, path: String) -> Uri
set_query(uri: Uri, query: String) -> Uri
set_fragment(uri: Uri, fragment: String) -> Uri
If okay'd, I'd be happy to go ahead and implement these myself.