mattam has quit [Read error: 110 (Connection timed out)]
Riastradh has quit ["migrating client to another machine"]
Riastradh has joined #ocaml
monochrom has quit ["hello"]
pango has quit [Remote closed the connection]
stef_ has quit [Read error: 110 (Connection timed out)]
pango has joined #ocaml
wam has joined #ocaml
_paganini has joined #ocaml
_paganini has left #ocaml []
Submarine has quit ["Leaving"]
sethk has joined #ocaml
mbh has joined #ocaml
<mbh>
Can anyone explain to me what Obj.magic does (or perhaps point me to some documentation)?
<Riastradh>
Magic!
* Riastradh
hides.
<Riastradh>
Obj.magic : 'a -> 'b
<Riastradh>
Or, in English, if you can't read & immediately understand the meanings of types as Smerdyakov asserts any ML programmer can: Obj.magic performs type coercion.
<sethk>
Riastradh, I'm afraid Smerdyakov didn't test me. :)
<Riastradh>
For example,
<Riastradh>
Obj.magic 3 +. 4.0
<mbh>
seg fault
<mbh>
hehe
<Riastradh>
(Obj.magic 3) there will have the type float. This will cause probably a bus error or something.
<mbh>
ok
<mbh>
so why is it used to often?
<mbh>
so*
<Riastradh>
Obj.magic is a type circumvention device, however; it will not perform object representation conversion.
<Riastradh>
It shouldn't be.
<Riastradh>
It is extremely dangerous.
<sethk>
Riastradh, I don't follow. 4.0 is a float, no? So why is that an error?
<sethk>
Riastradh, I know Haskell, am learning ocaml.
<Riastradh>
3 is not a float. Making OCaml think it is does a bad thing.
<Demitar>
mbh, it's not used often.
<sethk>
Riastradh, what's the correct way to use mixed types in that situation?
<Riastradh>
3 is an unboxed fixnum. Flonums in OCaml are, I believe, boxed, and therefore pointers.
<mbh>
yeah
* Demitar
now is in the posession of a gingerbread camel. :)
<Riastradh>
Use the correct numeric conversion operations, sethk.
<Riastradh>
(I don't remember what they are in OCaml.)
<sethk>
Riastradh, ok, then I'm back with mbh, what is it useful for (Obj.magic, that is).
<Demitar>
int_of_float / float_of_int are the basic ones.
<Riastradh>
Not much, sethk.
<Demitar>
In essence, if you think you need Obj.magic, then you don't.
<Riastradh>
The only legitimate use I've seen is in conjunction with the FFI, to convert objects of type Unix.file_descr to ints when passing fds to foreign functions.
<sethk>
Demitar, thanks. It's been two years since I've used Ocaml so the specifics I have to look up until I get more familiar with it.
<sethk>
Riastradh, as in interfacing to a different language? FFI is foreign function interface?
<Riastradh>
Yes.
<sethk>
Riastradh, thanks
<sethk>
Demitar, thanks also.
<Riastradh>
I have never used Obj.magic.
vezenchio has joined #ocaml
<Riastradh>
(Then again, I don't use OCaml much, but even if I did I'd never use it.)
<Riastradh>
s/it/Obj.magic/1
<mbh>
hmm
<sethk>
Riastradh, doesn't sound like a good thing from the type safety perspective. Segv is a pretty good hint about that. :)
<Demitar>
Obj.magic would defeat many of the reasons I use OCaml in the first place. :)
<Riastradh>
Exactly, sethk.
<Riastradh>
This is why I said 'It is extremely dangerous.' about Obj.magic.
<sethk>
Demitar, Riastradh, so the bottom line is that if you needed it, you would know why.
<Demitar>
sethk, exactly.
<Riastradh>
And you would have a _VERY_ good reason; otherwise you'd convince yourself otherwise.
<avlondono>
I've never used it, do you have any example?
<Demitar>
Anyone not being able to argue against the solid wall of "You don't need it." shouldn't even consider it. :)
* Riastradh
points up at his Unix fd example.
<mbh>
i'm tryint to use it to get the IEEE representation of a float
<mbh>
but it doesn't work to well:
<mbh>
Printf.printf "%x\n" (Obj.magic 12.)
<Demitar>
mbh, eh?
<Riastradh>
mbh, that won't get you the IEEE representation of a float. That will get you an integer representation of a pointer.
srv has quit [Remote closed the connection]
<mbh>
oh, float's are boxed?
<Riastradh>
Yes.
srv has joined #ocaml
<mbh>
ok
<mbh>
nvm
<Demitar>
mbh, that's a good example of a place where "You shouldn't use it." applies. :)
<mbh>
hehe
<Demitar>
I think your (type)safest bet would be a tiny C accessor.
<Riastradh>
You might get it right if you used Int64 instead of int.
<mbh>
yeah, that's what i ended up doing
<Riastradh>
Int64.add (Obj.magic 1.) Int64.zero
<Riastradh>
However, it is still very dangerous.
<mbh>
hmm
<Riastradh>
You should probably ask the mailing list for the best way to do this.
<Demitar>
mbh, why do you want this btw?
gim has quit []
<mbh>
Demitar: educational reasons
<Demitar>
As in learning OCaml or as in investigating IEEE floats?
<mbh>
IEE floats
<Demitar>
Good, in an evil way. ;-)
<mbh>
im trying to access the 5th element of an array like:
<Demitar>
Use C if you really want to do the conversion.
<mbh>
hehe
<mbh>
i know, i'm just messing around with Obj.magic :)
<mbh>
Obj.magic would be faster than a c call, right?
<Demitar>
Yes. But if it's educational, what makes that so important?
<Demitar>
(Obj.magic would take no time at all since it merely changes the typing rules, and type information is discarded.)
vezenchio has quit ["haibane · renmei"]
vezenchio has joined #ocaml
m3ga has joined #ocaml
m3ga has quit ["Client exiting"]
mrsolo_ has joined #ocaml
mrsolo_ has quit [Read error: 104 (Connection reset by peer)]
mrsolo_ has joined #ocaml
mrvn_ has joined #ocaml
mrvn has quit [Read error: 110 (Connection timed out)]
<mbh>
LK;
<mbh>
stupid irssi
m3ga has joined #ocaml
cryptilox has joined #ocaml
mlh has quit [Client Quit]
vezenchio has quit ["haibane · renmei"]
vezenchio has joined #ocaml
cmeme has joined #ocaml
cmeme has quit [Remote closed the connection]
cmeme has joined #ocaml
m3ga has quit ["Client exiting"]
m3ga has joined #ocaml
mattam has joined #ocaml
zzorn has joined #ocaml
zzorn is now known as zzorn_away
cryptilox has quit ["leaving"]
Smerdyakov has quit ["Client exiting"]
Submarine has joined #ocaml
pango has quit ["Client exiting"]
smimou has joined #ocaml
srv_ has joined #ocaml
srv has quit [Read error: 104 (Connection reset by peer)]
mbh has quit ["Lost terminal"]
pango has joined #ocaml
gim has joined #ocaml
m3ga has quit ["Client exiting"]
_fab has quit [Remote closed the connection]
smimou has quit ["?"]
johgro has joined #ocaml
srv_ has quit [Read error: 110 (Connection timed out)]
johgro has quit ["Leaving"]
karryall has joined #ocaml
stef has joined #ocaml
_fab has joined #ocaml
CosmicRay has joined #ocaml
vezenchio has quit ["haibane · renmei"]
vezenchio has joined #ocaml
mrvn has joined #ocaml
mrvn_ has quit [Read error: 60 (Operation timed out)]
Smerdyakov has joined #ocaml
karryall has quit ["."]
__DL__ has joined #ocaml
monochrom has joined #ocaml
Axioplase has joined #ocaml
<Axioplase>
hi
stef has quit [Nick collision from services.]
stef_ has joined #ocaml
KrispyKringle has joined #ocaml
smimou has joined #ocaml
gim_ has joined #ocaml
gim has quit [Read error: 104 (Connection reset by peer)]
gim_ is now known as gim
pango has quit [Remote closed the connection]
stef_ has quit [Read error: 60 (Operation timed out)]
stef_ has joined #ocaml
sethk has quit ["Got to go, the morlocks are calling"]
pango has joined #ocaml
vezenchio has quit ["haibane · renmei"]
<Axioplase>
Hi
<Axioplase>
isn't supposed "type typ= Var of var_type and var_type==string" supposed to be a legal declaration ?
<mrvn>
# type typ= Var of var_type and var_type=string;;
<mrvn>
type typ = Var of var_type
<mrvn>
and var_type = string
<Riastradh>
No. Notice the doubled = sign.
<Axioplase>
err.. then var_type=string makes var_type an "alias" for string, isn't it? (as for the doubled "=", then that was a typo in my lessons'pdf)
<mrvn>
yes
<Axioplase>
ok thanks
Submarine is now known as SubLeDoc
SubLeDoc is now known as Submarine
kinners has joined #ocaml
zzorn_away has quit ["They are coming to take me away ha ha"]
piyr has joined #ocaml
gpciceri has joined #ocaml
gpciceri has quit ["Leaving"]
mlh has joined #ocaml
__DL__ has quit ["Bye Bye"]
kinners has quit [Read error: 60 (Operation timed out)]