bluespec.com Forum Index bluespec.com
Bluespec Forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

struct with function member

 
Post new topic   Reply to topic    bluespec.com Forum Index -> Designing with BSV's Rules, Interfaces, ...
View previous topic :: View next topic  
Author Message
tspalink



Joined: 05 Nov 2015
Posts: 12

PostPosted: Wed Nov 11, 2015 6:10 pm    Post subject: struct with function member Reply with quote

I am attempting to use a struct with a function member field.

Naively I thought the following should work:

Code:
1  typedef struct {
2    function Bool bar(Bool baz);
3  } Foo;
4  Foo foo = Foo { bar: ? };


but it fails with the following error for line 2:

Quote:
Unexpected `;'; expected identifier starting with lowercase


Instead, the following code does compile, but I have no idea what it means:

Code:
typedef struct {
   function Bool bar baz;
} Foo;
Foo foo = Foo { baz: ? };


Could someone explain what is going on in both of my code examples?
Back to top
View user's profile Send private message
quark
Site Admin


Joined: 02 Nov 2007
Posts: 500

PostPosted: Wed Nov 11, 2015 6:30 pm    Post subject: Re: struct with function member Reply with quote

In the type declaration for struct fields (and also in the declaration of arguments to functions and modules, but the way), the format is this:
Code:
<type> <fieldname>;

For better or worse, this includes fields whose types are functions. The type in that case would be:
Code:
function <retType> f(<arg1Type>, ...)

Here, 'f' is a dummy name that exists to satisfy the parser, but doesn't really get used anywhere.

Putting it together, you'd define a fields with a function type like this:
Code:
function <retType> f(<arg1Type>, ...) <fieldname>;

In your case, it should be written like this:
Code:
function Bool f(Bool baz)  bar;

I don't know off-hand why this is the syntax that BSC accepts. Personally, I'd prefer to write it the way you originally attempted, which is more obvious and doesn't require an unused dummy name. (I suspect it's a bug that's been overlook because rarely has anyone written a struct with a function field. Typeclasses can often be used for the same purpose, and the syntax for typeclasses properly supports declaring function members.)

As for this:
Code:
function Bool bar baz;

My suspicion is that the parser is permitting the parentheses to be omitted when there are no arguments. So that is equivalent to this:
Code:
function Bool bar()  baz;

which conforms to the syntax that I explained above.
Back to top
View user's profile Send private message
tspalink



Joined: 05 Nov 2015
Posts: 12

PostPosted: Thu Nov 12, 2015 1:45 pm    Post subject: Reply with quote

Thanks!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    bluespec.com Forum Index -> Designing with BSV's Rules, Interfaces, ... All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum
bluespec.com topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP