@tiotolstoy has joined the channel
I’d much rather see a pr to add a version that works on arbitrary sequences
(struct foo (a))
defines a basic constructor function foo
where you need to supply all the parameters for all the struct fields.
What @laurent.orseau means by a “smart constructor” (I believe) is, you just also define your own function (say you call it make-foo
) that has any fancier behavior you want. Such as default parameters. Keyword parameters. A contract, or, manual validation. And so on.
I think @samth meant “unnecessary” more than “unhelpful”. All those things can be helpful. But you can handle it yourself, it doesn’t need to be baked into the core struct
.
Of course if you do this kind of thing a lot, you can write your own “define struct” macro that defines both a struct
and its fancy-constructor.
I think various people have done this. Various opinions how.
@jestarray @kellysmith12.21 ^
I specifically mean the default value feature that actually exists is unhelpful
A different feature, or a way to create smart constructors, would be useful, and many exist
The existing feature of struct is only useful for mutable fields that will only ever be set by a client not constructing the struct
Is there a way in a #lang scribble/manual
file to hide some of the for-label
bindings, so that @racketblock
doesn’t hyperlink them?
only-in ?
except-in ?
I didn’t require any particular bindings though; these are (I think) coming from the #lang line itself
Ah. Tricky.
Don’t know.
@blerner I don’t think that’s true. If you use #lang scribble/manual
without any for-label
, then nothing will be linked. You need to (require (for-label racket/base))
explicitly to at least get built-in bindings.
yeah, after tracing through our imports some more, it looks like there in fact was a require
d library for-label
that I hadn’t noticed. now that I see it, I can except-in
eliminate the parts I don’t want
Could you somehow use a dummy module that shadows the bindings you don’t want?
Ah nvm
Is it a bug that build-path/convention-type
cannot construct a Unix-style path when running on Windows?
yeah, was going to comment on the Pollen issue that you might want to report the problem lol
for pairs i need '(0 . (1 . 2))
where the 2nd element is a pair and the first is a value
but racket turns that into '(0 1 . 2)
…
i need the parenthesis there for the 2nd element because the serializer to a rust struct requires: (name . ( (first . "bob" ) (last . smith)))
the first element to be the name of the field, and the second to be the value
@franklai_slack has joined the channel
@jestarray The parentheses are implicit, however, you could write a custom print function to keep them.
im unsure how to go about doing so…
Oh, it’s for a serializer? That’s the format that needs to be read in?
yeppers. #[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
struct Person {
name: String,
age: u8,
};
// always in the format of (symbol . value). Trying to get (symbol . (symbol . value)) e.g (name . ((first . "bob) (last . smith))) working but racket excludes the parens needed
let joanne: Person = from_str("((name . \"Joanne\") (age . 23))").unwrap();
https://docs.rs/serde-lexpr/0.1.1/serde_lexpr/
Ah, I see.
In that case, I would suggest defining a Racket struct to represent a Rust struct. Then, you can define a function to serialize it.
I’ll write up a quick example.
gonna try to see if i can get it to print pairs with the parens first… the docs on it is a bit scruffy…
This function will print a list to the provided output-port, while keeping parens and middle dots: (define (print-pair/parens pr [port (current-output-port)])
(fprintf port
"'(~a . ~a)"
(car pr)
(let loop ([val (cdr pr)])
(if (pair? val)
(format "(~a . ~a)" (car val) (loop (cdr val)))
val))))
thanks, ill give it a shot
so close yet so far to near zero friction….
(If you don’t want the leading quote, then that function can be simplified.)
Treating (a b . c)
and (a . (b . c))
as equivalent is one of the things s-expression formats typically have in common. I bet serde-lexpr would accept either one.
That being said, it’s good to write explicit serializing code in situations like this. If Racket and serde-lexpr do turn out to differ on some aspect of the s-expression syntax, trying to rely on Racket’s write
will only get you so far.
i was using print
because it initially gave me the least resistance but now write
is turning out better now
just have to replace a few things
i could very easily swap this out with json which has a lot more work put into rust serialization and deserialization libs but ill mucking everything to s-expressions for consistency since the scripting language for my game will probably try to integrate racket or some scheme