PureBasic Forum
http://forums.purebasic.com/english/

[Done] Define and Read
http://forums.purebasic.com/english/viewtopic.php?f=37&t=71124
Page 1 of 1

Author:  Little John [ Fri Jul 27, 2018 10:23 pm ]
Post subject:  [Done] Define and Read

Please update the documentation of Define, since its behaviour has changed.

If no <variables> are specified, Define is used to change the default type for future untyped variables (including procedure parameters, interface method parameters and data to read with the Read keyword).
This (fortunately) does not work anymore, but is currently only documented in the version history:
History for PB version 5.60 wrote:
- Changed: removed 'define.b' syntax to change default type as it could create hard to find bugs.

The documentation of Read is related, and should also be changed. It mentions a default data type and contains a link to the documentation of Define.

The documentation should state clearly, that there is only one default data type (integer), and that this is not subject to change by the user.

Author:  Andre [ Sat Dec 08, 2018 12:00 am ]
Post subject:  Re: Define and Read

Something I'm not sure about the best way to describe / change the docs... :oops:

Any concrete suggestions?

Author:  Demivec [ Sat Dec 08, 2018 5:01 am ]
Post subject:  Re: Define and Read

Somethin like:
Code:
Define

Syntax
Define[.<type>] [<variable> [= <expression>], <variable> [= <expression>], ...]
Description
If no type is specified the default type of integer (.i) will be used. Each variable can have a default value directly assigned to it.

Example

  Define.b a, b = 10, c = b*2, d ; these 4 variables will be byte typed (.b)

Syntax
Define <variable>.<type> [= <expression>] [, <variable>.<type> [= <expression>], ...]
Description
Alternative possibility for the variable declaration using Define.
Example

  Define MyChar.c
  Define MyLong.l
  Define MyWord.w
 
  Debug SizeOf(MyChar)   ; will print 2
  Debug SizeOf(MyLong)   ; will print 4
  Debug SizeOf(MyWord)   ; will print 2

Author:  Little John [ Sat Dec 08, 2018 8:38 am ]
Post subject:  Re: Define and Read

I agree with Demivec's suggestion. Thank you!

Author:  Sicro [ Sat Dec 08, 2018 8:14 pm ]
Post subject:  Re: Define and Read

But that's also possible:
Code:
Define.q a, b.w

Debug SizeOf(a) ; will print 8
Debug SizeOf(b) ; will print 2, because it doesn't have the default type
Therefore I suggest the following syntax, which covers all possibilities:
Code:
Define[.<default_type>] <variable>[.<type>][=<expression>] [,<variable>[.<type>][=<expression>], ...]

Author:  Little John [ Sat Dec 08, 2018 8:24 pm ]
Post subject:  Re: Define and Read

Sicro wrote:
But that's also possible:
Code:
Define.q a, b.w

Yes, that's possible.
But it's no good style IMHO, so people shouldn't be encouraged to write something like that. :-)

Author:  #NULL [ Sat Dec 08, 2018 9:17 pm ]
Post subject:  Re: Define and Read

to be precise.. :)
Code:
Define.b a, b.w, c
Define d

Debug SizeOf(a) ; 1
Debug SizeOf(b) ; 2
Debug SizeOf(c) ; 1 (type .b and not .w or .i)
Debug SizeOf(d) ; 8 (on x64, 4 on x86)

Author:  Little John [ Sat Dec 08, 2018 9:44 pm ]
Post subject:  Re: Define and Read

My "favorite" is something like this: :lol:
Code:
Define.f x$ = "1.23"

Author:  #NULL [ Sat Dec 08, 2018 9:51 pm ]
Post subject:  Re: Define and Read

:mrgreen:

<edit>
Code:
Define.f x$ = "1.23" + "0.77" + " / 2"

Author:  Little John [ Sat Dec 08, 2018 10:08 pm ]
Post subject:  Re: Define and Read

:mrgreen:

Author:  Sicro [ Sat Dec 08, 2018 11:16 pm ]
Post subject:  Re: Define and Read

:mrgreen:

@Little John:
Yes, you're right. Using the keyword "Define" with default type and then defining a variable with a different type can easily be confusing / overlooked by programmers and is therefore not a good programming style.
But I think that a syntax description should always show all possibilities. The syntax variants that are not recommended should then also be mentioned in the description.

Author:  Lord [ Sun Dec 09, 2018 11:12 am ]
Post subject:  Re: Define and Read

Some ideas:
Code:
Define a
; a will be type integer, 4 byte on 32 bit, 8 byte on 64 bit

Define a, b, c
; a, b and c will be type integer, 4 byte on 32 bit, 8 byte on 64 bit

Define a, b.w, c
; should raise a syntax error, no type on b allowed

Define.w a
; a will be type word

Define.w a, b, c
; a, b, c will be of type word

Define.w a, b.i, c
; should raise an syntax error, no type on b allowed

Define.s a
; b will be of type string (= a.s)

Define.s a, b, c
; a, b and c will be of type string (= a.s, b.s, c.s)


; Maybe new:
Define.$ a
; a will be type string (= a$)

Define.$ a, b, c
; a, b, c will be of type string (= a$, b$, c$)

Define.$ a, b.s, c
; should raise an syntax error, no type mixing allowed (a.s <> a$)


In general: no mixing of definitions on same define statement.

Edit: corrected typing errors

Author:  Little John [ Sun Dec 09, 2018 11:43 am ]
Post subject:  Re: Define and Read

@Sicro:
I agree with you.

Author:  #NULL [ Sun Dec 09, 2018 11:56 am ]
Post subject:  Re: Define and Read

Lord wrote:
Some ideas:
Code:
Define a, b,w, c
; should raise a syntax error, no type on b allowed
should be .w not ,w? I don't like the idea though!
Quote:
Code:
Define.w a, b.i, c
; should raise an syntax error, no type on b allowed

Define.s a, b.i, c
; a, b and c will be of type string (= a.s, b.s, c.s)
Kind of contradictory and i don't like either of them.

Author:  Demivec [ Sun Dec 09, 2018 5:03 pm ]
Post subject:  Re: Define and Read

I am in favor of the current ability to mix types on the same line when no default type is specified (i.e. 'Define j.b, k, m.f') but would be in favor of removing the ability to have distinct default along with specific types (i.e. 'Define. b j.f, k, m') because of its potential for confusion and error.

Page 1 of 1 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/