Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

“Option” is the operative word. A type that does not implement the correct trait cannot be used in a way that requires the trait. If you are writing anything non-trivial that would probably be preferable. It requires nothing, that is the default behavior, so I did not bother to mention it.


sort by: page size:

The options for the thing being constructed are all separate types from the thing being constructed; the options aren’t a facet of the definition of the type they mutate.

(E is not required to implement that trait, it just often does)

As long as you're adding a type system (optional or not) it does seem a bit odd to opt for an unsound one...

That's not how these trait/type systems work.

Optional types. It's literally says in the thread title.

I think the point is that `&TraitFoo` would never be expected - that there was no way to have that be the type of a term in the first place? I don't think that can reasonably be said to be true in the days of impl trait though.

Whenever i design a lang, i just give option semantics to types of the form `x|none`

I think that when the OP mentioned optional typing, they were probably thinking of a gradual type system. So there would be a design choice to fall back to an ‘any’ type when a better type cannot be inferred. I would also assume that the OP is also not requesting higher ranked types, which also makes some things easier.

That's not true. You can implement your own traits for foreign types.

Agreed. This is not "mandatory types", note well.

Whoops, I always thought I can use any as type. Damn my bad

Types don't tell you if the behavior is correct or not.

Some languages don't care either way, and will let you add those types.

If you change a type from T to Option<T>, yes, all the other code has to change to take the option into account.

Yea, traits make this much simpler to avoid. The problematic type would not match the trait, likely leading to much simpler communication of the error.

optional typing though?

Absolutely. Sometimes the type is too complex not to be explicit about it.

It's more a traits system than a type system. Who cares if it's a list so long as I can append to it?

Yes, the type system is clearly not well integrated.

My point is, I'd rather have a not so perfect one when I need it, that None at all.

And most of the time I don't need one.

next

Legal | privacy