Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default value for primitives #5113

Closed
mwambler opened this Issue Oct 5, 2015 · 16 comments

Comments

Projects
None yet
6 participants
@mwambler
Copy link

mwambler commented Oct 5, 2015

Working through some code samples while getting familiar with typescript, and have noticed that typescript doesn't assign default values to primitive types(e.g. counter: number == NaN, b: boolean == undefined). Would it make sense to add this behavior at compile time, or maybe add a compiler setting to set appropriate default values (e.g. counter : number = 0, b :boolean= false)?

thanks

-Mark

@kitsonk

This comment has been minimized.

Copy link
Contributor

kitsonk commented Oct 5, 2015

I think that would be surprising behaviour to anyone who as worked with JavaScript as defined variables without a value are set to undefined. What is the reasoning for changing that behaviour?

@mwambler

This comment has been minimized.

Copy link
Author

mwambler commented Oct 5, 2015

I had the opposite reaction. Statically typed languages typically set primitives to a default value unless explicitly set. Not a big deal, I can certainly adjust. Thanks for responding...

-Mark

@EvertonLenger

This comment has been minimized.

Copy link

EvertonLenger commented Aug 17, 2017

Wouldn't this be useful?

I don't really know if it's been planning to implement this, but I think it'd be very useful.

When I declare a variable as a number, I'd prefer its value to be 0, not undefined.

@kitsonk

This comment has been minimized.

Copy link
Contributor

kitsonk commented Aug 17, 2017

useful in this sense, is very much an opinion. Code that wrote itself would be useful. Changing the default behaviour of an established language that is an extension of another language isn't useful, it is crazy.

@EvertonLenger

This comment has been minimized.

Copy link

EvertonLenger commented Aug 18, 2017

Yeah, I understand you. After I read your response, I see that it would have more downsides than benefits.

Thanks for your quick response!

@mwambler

This comment has been minimized.

Copy link
Author

mwambler commented Aug 18, 2017

In addition to JavaScript, TypeScript borrows from Java, C#, others. Why would it be "crazy" to want a compiler switch to set default primitive values consistent with those other languages?

@RyanCavanaugh

This comment has been minimized.

Copy link
Member

RyanCavanaugh commented Aug 18, 2017

TypeScript doesn't change the runtime behavior of JavaScript code. It's not "crazy" like a madman, but it's not something we do as a language.

@jez9999

This comment has been minimized.

Copy link

jez9999 commented Oct 5, 2017

This doesn't change the default behaviour of JavaScript because JS doesn't have the concept of a primitive type. If TypeScript requires that a boolean type be set to something (and it does), it can figure out that if it is not specified it should be set to false.

@kitsonk

This comment has been minimized.

Copy link
Contributor

kitsonk commented Oct 5, 2017

This doesn't change the default behaviour of JavaScript because JS doesn't have the concept of a primitive type.

That is incorrect... JavaScript is a weakly typed language, not un-typed language. Primitive types are passed into functions by value, while non-primitives are passed by reference. The typeof operator in JavaScript explicitly informs you of what the type of a variable is (hint is in the name of the operator). There are 5 primitive types (string, number, boolean, undefined, symbol) and two non-primitives (object, function) that are available in user land.

@jez9999

This comment has been minimized.

Copy link

jez9999 commented Oct 5, 2017

OK, I guess my point was that when you assign a number to a variable the variable changes type from undefined to number, rather than already being a number that can somehow hold undefined. Given that the primitive types have to hold some value, it is not surprising to assign a default value automatically.

@kitsonk

This comment has been minimized.

Copy link
Contributor

kitsonk commented Oct 5, 2017

Given that the primitive types have to hold some value, it is not surprising to assign a default value automatically.

It is surprising.

@jez9999

This comment has been minimized.

Copy link

jez9999 commented Oct 5, 2017

How so? The primitive cannot hold undefined. It is an error to try and set it to undefined. Why would you expect it to be undefined?

@kitsonk

This comment has been minimized.

Copy link
Contributor

kitsonk commented Oct 5, 2017

undefined is a primitive so what you are saying makes no sense at all... It is surprising for an uninitialised variable to have a value other than undefined because that is the behaviour of JavaScript. Auto-magically assigning values to variables has a performance and memory impact on the run-time code.

TypeScript is about developer being more explicit about their code, not less. If a TypeScript user wants a variable to be initialised, they should initialise the variable.

@jez9999

This comment has been minimized.

Copy link

jez9999 commented Oct 5, 2017

You're being more explicit about the type of the variable. It's a number for instance. Once you've established that it makes sense to initialize it to a default value (which is why most other languages do it).

@EvertonLenger

This comment has been minimized.

Copy link

EvertonLenger commented Oct 5, 2017

When I've commented for the first time, I had faced the following situation: I had a function that accepted a number as a parameter. Well, I had to test if this value was undefined or not. In my opinion, as I had setted the type number to the parameter, I thought it would be useful if I hadn't had the obligation to check that. And, in this way, it would be more explicit for the developers too.

@kitsonk

This comment has been minimized.

Copy link
Contributor

kitsonk commented Oct 5, 2017

which is why most other languages do it

TypeScript non-goal:

  1. Exactly mimic the design of existing languages. Instead, use the behavior of JavaScript and the intentions of program authors as a guide for what makes the most sense in the language.

@EvertonLenger

Well, I had to test if this value was undefined or not. In my opinion, as I had setted the type number to the parameter, I thought it would be useful if I hadn't had the obligation to check that.

That is either solved by using default arguments (part of ECMAScript) or strict null checks (part of TypeScript).

function foo(value = 0) {
  value; // always a number that is initalised 😱
}

// @strictNulChecks: true
function foo(value?: number) {
  value.toString(); // Object is possibly undefined, forced to check
}

@Microsoft Microsoft locked and limited conversation to collaborators Jun 19, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.