Skip to content

Instantly share code, notes, and snippets.

@matthew-piziak
Created August 23, 2016 20:48
Show Gist options
  • Save matthew-piziak/a2dd18e897eb3d4113459a945d85296b to your computer and use it in GitHub Desktop.
Save matthew-piziak/a2dd18e897eb3d4113459a945d85296b to your computer and use it in GitHub Desktop.
12:47 pm imperio
matthew-piziak-: I didn't pay enough attention to your other example, but all Bit* traits have a complicated implementation because you use a vector :-/
12:47 pm breeden
imperio: perhaps he could keep these examples but just add a simple example first?
12:47 pm imperio
breeden: no no, the point is to demonstrate what the trait does
12:47 pm breeden
k
12:48 pm imperio
I don't see any interest in having something more complicated than necessar
12:48 pm
if that was a complex function with some special cases, then yes
12:48 pm
but it's not the case in here
12:48 pm
jntrnr: do you mind if I ask indentation for https://github.com/rust-lang/rust/pull/35939/fi... ? :)
12:49 pm matthew-piziak-
`enum Bool { True, False }` and `enum Bool { True, False, FileNotFound }` are both good examples for `And`, not `BitAnd`.
12:49 pm jntrnr
imperio: up to you if you're reviewing
12:49 pm imperio
you're the reviewer
12:49 pm matthew-piziak-
Intuitively `&` is a string operation, not a scalar operation.
12:49 pm
`&&` is a scalar operation.
12:49 pm steveklabnik
http://thedailywtf.com/articles/What_Is_Truth_0...
12:50 pm
is the reference
12:50 pm
incidentally
12:50 pm
heh
12:50 pm matthew-piziak-
:P
12:50 pm
Thanks - I didn't realize, haha.
12:50 pm peschkaj
haha
12:50 pm matthew-piziak-
Anyway, a boolean of vectors was the simplest example I could think of.
12:50 pm imperio
& is a string operation?
12:50 pm matthew-piziak-
Because it's the simplest string of booly items.
12:50 pm imperio
what does it mean?
12:50 pm jntrnr
imperio: ah, my email must be slow
12:50 pm matthew-piziak-
By "string" I mean a set of items, plus well order.
12:51 pm
An iterable, if you will.
12:51 pm imperio
jntrnr: I go on github notifications once an hour
12:51 pm
matthew-piziak-: not sure to understand but whatever
12:51 pm breeden
matthew-piziak-: i agree. It's a very pratical example.
12:51 pm imperio
for me, both && and & are binary operators
12:51 pm matthew-piziak-
They are indeed both binary.
12:52 pm
To me, '&' is zipped '&&'.
12:52 pm
"bit" means "booly", "wise" means zip, "and" means "and".
12:52 pm jntrnr
imperio: looks okay to me. The indentation matches what was already there
12:53 pm
imperio: I mean they could indent both, but seems a little nitty
12:53 pm imperio
jntrnr: then I don't have anything to say :p
12:53 pm matthew-piziak-
I do realize that I'm being maximally pedantic right now.
12:53 pm imperio
matthew-piziak-: a bit too much yes haha
12:54 pm matthew-piziak-
So, the question is: are we okay overloading '&' to be a synonym of '&&'?
12:54 pm
I'm one for keeping their meanings distinct.
12:57 pm imperio
their different
12:57 pm
& compare bits and && compare objects
12:57 pm
s/their/they're
12:58 pm matthew-piziak-
If that were always the case, then `&` would not be overloadable, right?
12:59 pm imperio
it would be strange if it wasn't overloadable
12:59 pm
but you're not supposed to overload it normally
1:00 pm
or only if it's on low level type
1:00 pm matthew-piziak-
hm
1:00 pm
Let me show you an example from Haskell.
1:00 pm
In GHCI:
1:00 pm imperio
sure
1:00 pm matthew-piziak-
`:t zipWith (&&)`
1:00 pm
output: `zipWith (&&) :: [Bool] -> [Bool] -> [Bool]`
1:02 pm
`import Data.Bits; :t (.&.)`
1:02 pm
output: `(.&.) :: Bits a => a -> a -> a`
1:04 pm
Any simpler example for overloading `&` is going to be indistinguishable from overloading `&&`.
1:05 pm
Unless I do something like lifting the bitwise operation onto a trivial struct tuple like I do with `Shl` and `Shr`.
1:05 pm
I could do that.
1:06 pm imperio
that might be a good way to do it
1:06 pm matthew-piziak-
That's true for every trait though - doesn't have any flavor to it.
1:07 pm
Here's another way of looking at it: for a single bit, bitwise AND and regular AND are identical.
1:07 pm
"bitwise AND" implies a traversal over ANDable values.
1:07 pm imperio
on this level yes
1:09 pm matthew-piziak-
I think that "bitwise AND" has distinct semantic value that extends beyond lifting the operation to a wrapper type.
1:09 pm
But I do think I should include that example, like I did with `Shl` and `Shr`.
1:09 pm imperio
hmmmmmmmm
1:09 pm
steveklabnik: ^
1:21 pm ubsandroid
matthew-piziak: the important difference is that && is short circuiting
1:21 pm
that's it
1:22 pm
it only makes sense for bool, therefore
1:24 pm matthew-piziak-
Huh. I only just realized that `&&` and `||` are not overloadable operators.
1:39 pm matthew-piziak
The `BitAnd` example is already merged in, but I'm going to try and whip up a PR that modifies it to address some of these points. Keeping sending your thoughts as you have them though!
1:39 pm
I'm starting to wonder if all operators should start with a simple "lifting" example that lifts the operator to a trivial tuple struct.
1:40 pm
might be a nice standardization
1:42 pm
And many implementers might be looking to see exactly that. It answers the basic questions: Does it have an associated type? Does it require a mutable reference? Then following examples can demonstrate more semantics of the operator, like SystemTime - Duration, or "inverty enums", etc.
1:46 pm
For `BitAnd`, a simple struct with a `bits` field might be nice. And I think the exposition should include some of the points we've been making here: that `&&` cannot be overloaded because it implies short-circuiting, which cannot be guaranteed by the implementer; that `&` can take on the semantics of non-short-circuiting `&&`; that it may be more suitable for low-level types; that it can also be used to imply zipping, as it does wit
1:46 pm
bits...
1:46 pm
Some may be more appropriate as module-level documentation as well, depending.
1:46 pm
The point is I'm not sure, but I'll try throwing something up in the meantime as a focus for discussion.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment